CoreData 更新无法使用 MagicalRecord

CoreData update not working using MagicalRecord

我正在尝试使用 MagicalRecord(版本 2.3)更新现有的 CoreData 实体;这是我的代码:

PreferenceData *updateData = [PreferenceData MR_createEntityInContext:defaultContext];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"aHourFormat == %@ || aHourFormat == %@", [NSNumber numberWithInt: 12], [NSNumber numberWithInt: 24]];
updateData = [PreferenceData MR_findFirstWithPredicate: predicate inContext: defaultContext];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *staffNamesArray = [[userDefaults arrayForKey:@"staffNamesArray"] mutableCopy];
NSMutableDictionary *preferencesDict = [[userDefaults dictionaryForKey:@"preferencesDictionary"] mutableCopy];

NSMutableArray *addlSvcsArray = [self createAddlSvcsTextboxArray];
NSMutableArray *customServicesArray = [[userDefaults arrayForKey:@"customServicesArray"] mutableCopy];

updateData.aStaffPos1 = staffNamesArray[0];
updateData.aStaffPos2 = staffNamesArray[1];
updateData.aStaffPos3 = staffNamesArray[2];
updateData.aStaffPos4 = staffNamesArray[3];
updateData.aStaffPos5 = staffNamesArray[4];
updateData.aStaffPos6 = staffNamesArray[5];

updateData.aShopOpens = [preferencesDict objectForKey:@"shopOpens"];
updateData.aShopCloses = [preferencesDict objectForKey:@"shopCloses"];

updateData.aDeleteOldAppts = [NSNumber numberWithInteger:[oDeleteAfterXDays.text intValue]];
updateData.aHourFormat = [NSNumber numberWithInt:[preferencesDict objectForKey:@"timeFormat"]];
updateData.aServicesType = [NSNumber numberWithInt:[preferencesDict objectForKey:@"servicesType"]];
updateData.aShowApptServices = [NSNumber numberWithInteger: @0];
updateData.aColorScheme = [preferencesDict objectForKey:@"colorScheme"];
updateData.aUpdatedRecordFlag = [NSNumber numberWithInteger: @1];

updateData.aAddlSvcs1 = addlSvcsArray[0];
updateData.aAddlSvcs2 = addlSvcsArray[1];
updateData.aAddlSvcs3 = addlSvcsArray[2];
updateData.aAddlSvcs4 = addlSvcsArray[3];
updateData.aAddlSvcs5 = addlSvcsArray[4];
updateData.aAddlSvcs6 = addlSvcsArray[5];

[defaultContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
    if(success)
        NSLog(@"update successful");
    else
        NSLog(@"update failed");
}];

}

我没有收到任何错误;数据在变量中,但是当我使用 sqlitebrowser 查看记录时,没有任何改变。

我做错了什么?

您首先在 updateData 中创建一条新记录,然后用搜索结果覆盖该变量。每次 运行 更新时,您都会创建一个新记录,该记录将只有默认值或为空。

如果您的数据没有按预期更新,您获取的 updateData 很可能是 nil,因为谓词有问题。