我已保存的對象(持久)之前依然存在。可以說下面是值:RESTKit:GET對象局部比較核心數據覆蓋
//Entity: employee
objectID: 1111
firstName: @"Jon"
lastName: @"D"
modified: @"10:45PM"
現在,我做一個RKManagedObjectRequestOperation *operation
請求。
我設置operation.savesToPersistentStore = NO;
並開始運作。
對象被下載,它已修改爲以下幾點:
//Entity: employee
objectID: 1111
firstName: @"Jonathan"
lastName: @"Doe"
modified: @"10:55PM"
//I have 15 other properties that are either Strings, NSDate, NSNumber, and BOOLs
我相信在這個時候修改的對象是在managedObjectContext
如果modified
日期是Context
不同,我想要比較Context
中的每個attribute
與核心數據中保留的那個。而如果Context
attribute
是一個比持久不同,我需要觸發一個標誌,每個屬性屬性。一旦所有的屬性都被竄改,我可以通過保存上下文來覆蓋對象。
我想我將不得不手動檢查:
if (!([objectInCoreData valueForKey:@"firstName"] isEqualToString:[objectInContext [email protected]"firstName"]))
{
firstNameValueChange = YES; // Trigger this flag
// I have to trigger a flag for each attribute that's changed to show
// the changed value in the tableView in a different color
}
//Continue to check for each attribute, and at end overwrite persisted with Context by
[self.managedObjectContext save:&error];
問題1:我有一種感覺,有一種更好的方式來做到這一點。最佳做法是什麼?
我如何可以簡化檢查15個不同屬性的代碼?
問題2:我在哪裏測試,如果每個屬性發生了變化?在successBlock裏面? wilSave:方法?
**更新*
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = appDelegate.managedObjectStore.managedObjectCache;
operation.savesToPersistentStore = NO;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSSet *updatedObjects = [self.managedObjectContext updatedObjects];
NSLog(@"updatedObjects Count %i", updatedObjects.count);
NSArray *_updatedObjects = updatedObjects.allObjects;
NSLog(@"_updatedObjects Count %i", _updatedObjects.count);
for (int i = 0; i<_updatedObjects.count; i++)
{
Invite *invite = [_updatedObjects objectAtIndex:i];
NSDictionary *changedValues = [invite changedValues];
}
}failure:^(RKObjectRequestOperation *operation, NSError *error) {}
登錄
I restkit.network:RKObjectRequestOperation.m:250 GET 'http://www.domain.com/api?lastrequest=2014-04-22T07%3A06%3A34Z' (200 OK/28 objects) [request=0.1140s mapping=0.0865s total=0.3034s]
2014-04-29 07:06:43.112 App[10627:60b] updatedObjects Count 0
2014-04-29 07:06:43.112 App[10627:60b] _updatedObjects Count 0
我可以看到,我得到幾個對象後面的日誌,但一切都爲空或0。我猜測我的MOC是空白的。對象如何從mappingResult
到MOC
?
謝謝!我對這個問題做了更新,你能告訴我,如果我走在正確的道路上嗎?我錯過了什麼B/C一切都是作爲0或null。 – user1107173
你有沒有得到'updatedObjects'或者它是空的?這可能是MOC被保存,但沒有傳播到持久存儲... – Wain
我添加了日誌。 updatedObjects是空的。我得到28個物體。我有SQLite管理器,我沒有看到添加到商店的任何更改。 – user1107173