iOS从数组导入的魔法记录 [英] iOS Magical record import from array
问题描述
您好,我收到 JSON
响应从服务器更新数据库的同步功能。如果有不同的数据(新记录或更新现有记录)(为了提高性能)(使用 coredata
和 magicalRecord
)
这是我的JSON解析器方法
- (void)updateWithApiRepresentation:(NSDictionary *)json
{
self.title = json [@Name];
self.serverIdValue = [json [@Id] integerValue];
self.year = json [@Year of Release];
self.month = json [@Month of Release];
self.day = json [@Day of Release];
self.details = json [@Description];
self.coverImage = json [@CoverImage];
self.thumbnail = json [@Thumbnail];
self.price = json [@Buy];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@dd / MMMM / yyy];
NSDate * date = [formatter dateFromString:[NSString stringWithFormat:@%@ /%@ /%@,self.day,self.month,self.year]];
self.issueDate = date;
}
而我的导入方法
+(void)API_getStampsOnCompletion:(void(^)(BOOL success,NSError * error))completionBlock
{
[ApiClient sharedInstance] getStampsOnSuccess: (id responseJSON){
NSManagedObjectContext * localContext = [NSManagedObjectContext MR_context];
NSMutableArray * stamps = [[NSMutableArray alloc] init];
[responseJSON [@root] enumerateObjectsUsingBlock:^(id attributes,NSUInteger idx,BOOL * stop){
Stamp * stamp = [[Stamp alloc] init];
[stamp setOrderingValue:idx];
[stamp updateWithApiRepresentation:attributes];
[stamps addObject:stamp];
}];
[Stamp MR_importFromArray:stamps inContext:localContext];
} onFailure:^(NSError * error){
if(completionBlock){
completionBlock(NO,error);
}
}];
}
我收到错误
$ b $
CoreData:error:无法调用NSManagedObject类的指定初始化器
2016-08-02 23:52:20.216 SingPost [2078:80114] - [邮件setOrdering:]:无法识别的选择器发送到实例0x78f35a30
我检查了我的Json解析器工作正常。问题是我的导入方法。我不知道这个函数出了什么问题。任何帮助是非常感激。感谢!
解决方案错误消息清楚地描述了确切的问题。你这样做:
Stamp * stamp = [[Stamp alloc] init];
但 init
不是指定的初始值 NSManagedObject
,除非你在你的子类中添加了 init
(你没有提到)。您必须调用指定的初始化程序,即 initWithEntity:insertIntoManagedObjectContext:
。还有一个方便的工厂方法在 NSEntityDescription
调用 insertNewObjectForEntityForName:inManagedObjectContext:
。任何一个都可以工作,但是调用 init
不会。
Hi I'm making a synchronise function that update database when receive JSON
response from server. I want the import only take place if there are different data (new record or update existing record) (To increase performance) (Using coredata
and magicalRecord
)
Here is my JSON parser method
- (void)updateWithApiRepresentation:(NSDictionary *)json
{
self.title = json[@"Name"];
self.serverIdValue = [json[@"Id"] integerValue];
self.year = json[@"Year of Release"];
self.month = json[@"Month of Release"];
self.day = json[@"Day of Release"];
self.details = json[@"Description"];
self.coverImage = json[@"CoverImage"];
self.thumbnail = json[@"Thumbnail"];
self.price = json[@"Buy"];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"dd/MMMM/yyy"];
NSDate *date = [formatter dateFromString:[NSString stringWithFormat:@"%@/%@/%@",self.day,self.month,self.year]];
self.issueDate = date;
}
And my import method
+ (void)API_getStampsOnCompletion:(void(^)(BOOL success, NSError *error))completionBlock
{
[[ApiClient sharedInstance] getStampsOnSuccess:^(id responseJSON) {
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_context];
NSMutableArray *stamps = [[NSMutableArray alloc]init];
[responseJSON[@"root"] enumerateObjectsUsingBlock:^(id attributes, NSUInteger idx, BOOL *stop) {
Stamp *stamp = [[Stamp alloc]init];
[stamp setOrderingValue:idx];
[stamp updateWithApiRepresentation:attributes];
[stamps addObject:stamp];
}];
[Stamp MR_importFromArray:stamps inContext:localContext];
} onFailure:^(NSError *error) {
if (completionBlock) {
completionBlock(NO, error);
}
}];
}
I'm getting error
CoreData: error: Failed to call designated initializer on NSManagedObject class 'Stamp'
2016-08-02 23:52:20.216 SingPost[2078:80114] -[Stamp setOrdering:]: unrecognized selector sent to instance 0x78f35a30
I have checked my Json parser is working fine. The problem is with my import method. I don't know what wrong with the function. Any help is much appreciate. Thanks!
解决方案 The error message clearly describes the exact problem. You do this:
Stamp *stamp = [[Stamp alloc]init];
But init
is not the designated initializer for NSManagedObject
unless you added init
in your subclass (which you didn't mention doing). You must call the designated initializer, which is initWithEntity:insertIntoManagedObjectContext:
. There's also a convenience factory method on NSEntityDescription
called insertNewObjectForEntityForName:inManagedObjectContext:
. Either of those will work, but calling init
will not.
这篇关于iOS从数组导入的魔法记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!