0
您好我正在做一個同步功能,當從服務器收到JSON
響應時更新數據庫。我想進口才會發生,如果有不同的數據(新記錄或更新現有記錄)(爲了提高性能)(使用coredata
和magicalRecord
)iOS從數組導入魔法記錄
這裏是我的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解析器工作正常。問題在於我的導入方法。我不知道這個函數有什麼問題。任何幫助非常感謝。謝謝!
嗨,感謝您的幫助,我正在與魔法記錄庫。是以上等於郵票*郵票= [郵票MR_createEntityInContext:localContext];?在添加新記錄時避免重複的任何方法? –
我沒有使用魔法記錄,所以我不能確定。 –
那麼你知道如何避免添加重複記錄(在收到來自服務器的json響應之後)嗎? –