我试图通过获取所有对象来使用魔法记录更新NSManagedObjects,然后使用for循环更改每个对象,并在保存上下文后没有更改
NSArray * locationArray = [[NSArray alloc]initWithArray:[Locations findAllInContext:[NSManagedObjectContext contextForCurrentThread]]];
for(Locations * currentLocation in locationArray)
{
currentLocation.name = @"Hello world";
}
[[NSManagedObjectContext contextForCurrentThread]saveToPersistentStoreAndWait];
答案 0 :(得分:0)
您只需将代码放入MagicalRecord的保存块
即可[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
// This block runs in background thread
NSArray * locationArray = [[NSArray alloc]initWithArray:[Locations findAllInContext:[NSManagedObjectContext contextForCurrentThread]]];
for(Locations * currentLocation in locationArray){
currentLocation.name = @"Hello world";
}
} completion:^(BOOL success, NSError *error) {
// This block runs in main thread
}];