我有这个代码,为我节省了一个我从后端收到的城市,它第一次完美无缺:
+ (void)saveCurrentCityWithParameters:(NSDictionary *)parameters
withSuccess:(OperationCompletionBlock)successBlock
withFailure:(OperationFailureBlock)failure
{
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
KNCity *knCity = [KNCity MR_findFirstInContext:localContext];
if (!knCity)
{
knCity = [KNCity MR_createInContext:localContext];
}
knCity.city_id = [NSNumber integerString:parameters[@"id"]];
knCity.city_lat = [NSNumber doubleString:parameters[@"lat"]];
knCity.city_lng = [NSNumber doubleString:parameters[@"lng"]];
knCity.city_name = parameters[@"name"];
} completion:^(BOOL success, NSError *error) {
[KNFilterHelper disableAllFilters];
[self requestFiltersWithSuccess:successBlock
withFailure:failure];
}];
}
第二次它也很完美但是如果应用程序仍然在运行,如果我从活动进程中删除它,它就不起作用。
我应该保存吗?
第一次跑:
(lldb) po localContext
<NSManagedObjectContext: 0x7a351150>
(lldb) po knCity
<KNCity: 0x78fbc960> (entity: KNCity; id: 0x78fb5340 <x-coredata:///KNCity/t2C6E56FF-7DD0-4505-A8A2-974EA687A87A3> ; data: {
"city_id" = 0;
"city_lat" = 0;
"city_lng" = 0;
"city_name" = nil;
})
第一次填充属性后
(lldb) po knCity
<KNCity: 0x78fbc960> (entity: KNCity; id: 0x78fb5340 <x-coredata:///KNCity/t2C6E56FF-7DD0-4505-A8A2-974EA687A87A3> ; data: {
"city_id" = 26;
"city_lat" = "51.151817";
"city_lng" = "71.480124";
"city_name" = "\U0410\U0441\U0442\U0430\U043d\U0430";
})
改变城市后:
(lldb) po localContext
<NSManagedObjectContext: 0x78f8a230>
(lldb) po knCity
<KNCity: 0x78fd37c0> (entity: KNCity; id: 0x78fc1110 <x-coredata://38CD5A19-1361-438E-8FFE-0DC5388B737C/KNCity/p1> ; data: <fault>)
答案 0 :(得分:0)
如果没有更多信息,我的建议是检查错误对象,看看您的数据是否未经过验证。例如,当属性不是可选属性并将其设置为nil时,整个保存操作将失败。这将导致您描述的症状,即您在应用程序的运行之间看不到数据。