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);
}
}];
}
我遇到错误
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
我已经检查过我的 Json 解析器工作正常。问题出在我的导入方法上。我不知道这个功能有什么问题。非常感谢任何帮助。谢谢!
错误消息清楚地描述了确切的问题。你这样做:
Stamp *stamp = [[Stamp alloc]init];
但是 init
不是 NSManagedObject
的指定初始值设定项,除非您在子类中添加了 init
(您没有提到这样做)。您必须调用指定的初始化程序,即 initWithEntity:insertIntoManagedObjectContext:
。 NSEntityDescription
上还有一个名为 insertNewObjectForEntityForName:inManagedObjectContext:
的便捷工厂方法。这些都可以,但调用 init
不会。
您好,我正在制作一个同步功能,当收到来自服务器的 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);
}
}];
}
我遇到错误
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
我已经检查过我的 Json 解析器工作正常。问题出在我的导入方法上。我不知道这个功能有什么问题。非常感谢任何帮助。谢谢!
错误消息清楚地描述了确切的问题。你这样做:
Stamp *stamp = [[Stamp alloc]init];
但是 init
不是 NSManagedObject
的指定初始值设定项,除非您在子类中添加了 init
(您没有提到这样做)。您必须调用指定的初始化程序,即 initWithEntity:insertIntoManagedObjectContext:
。 NSEntityDescription
上还有一个名为 insertNewObjectForEntityForName:inManagedObjectContext:
的便捷工厂方法。这些都可以,但调用 init
不会。