这个想法是将服务器JSON响应导入核心数据而不阻塞主线程上的UI,之后我仍需要导入的实体,经过整个上午的测试/谷歌搜索后,我找不到正确的方法来执行此操作。
__block NSMutableArray *cars;
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *carObject in carObjects) {
Notification *car = [Notification MR_importFromObject:carObject inContext:localContext];
[cars addObject:car];
}
} completion:^(BOOL success, NSError *error) {
if (success) {
for (Car *car in cars) {
// data may have invalid data or be nil
// [Car findAll] will have correct data though
}
}
}];
答案 0 :(得分:0)
我真的不知道导入完成的上下文,但UI阻止问题已经消失。
__block NSMutableArray *cars;
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *carObject in carObjects) {
Notification *car = [Notification MR_importFromObject:carObject]; // not pass in localContext
[cars addObject:car];
}
} completion:^(BOOL success, NSError *error) {
if (success) {
for (Car *car in cars) {
// data may have invalid data or be nil
// [Car findAll] will have correct data though
}
}
}];