flashfabrixx
flashfabrixx

Reputation: 1183

RestKit - Cache saved to Core Data only on second call

I'm loading edited objects from the server with RestKit (0.10.0) and Core Data in the backend with the following method. The method as part of a sync process is called whenever the app enters the foreground.

[syncObjectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"?config=accounts&since=%@", lastSync] usingBlock:^(RKObjectLoader *loader) {
    [loader.mappingProvider setObjectMapping:companyMappingSync forKeyPath:@"data"];
    loader.backgroundPolicy = RKRequestBackgroundPolicyContinue;
    loader.delegate = self;
}];

The response is loaded normal and the cache seemed to be found as well.

2012-04-11 15:58:32.147 mobileCRM[3575:707] T restkit.support:RKCache.m:82 Found cachePath '/var/mobile/Applications/C5E4BF4F-4CB0-4F4C-AC11-FC1E17AE4AF2/Library/Caches/RKClientRequestCache-www.URLTOSERVER.de/PermanentStore/37abc4aff62918578288d10530e6bcd6' for PermanentStore/37abc4aff62918578288d10530e6bcd6
2012-04-11 15:58:32.152 mobileCRM[3575:707] T restkit.support:RKCache.m:119 Wrote cached data to path '/var/mobile/Applications/C5E4BF4F-4CB0-4F4C-AC11-FC1E17AE4AF2/Library/Caches/RKClientRequestCache-www.URLTOSERVER.de/PermanentStore/37abc4aff62918578288d10530e6bcd6'
2012-04-11 15:58:32.158 mobileCRM[3575:707] T restkit.support:RKCache.m:100 Writing dictionary to cache key: 'PermanentStore/37abc4aff62918578288d10530e6bcd6.headers'
2012-04-11 15:58:32.159 mobileCRM[3575:707] T restkit.support:RKCache.m:82 Found cachePath '/var/mobile/Applications/C5E4BF4F-4CB0-4F4C-AC11-FC1E17AE4AF2/Library/Caches/RKClientRequestCache-www.URLTOSERVER.de/PermanentStore/37abc4aff62918578288d10530e6bcd6.headers' for PermanentStore/37abc4aff62918578288d10530e6bcd6.headers
2012-04-11 15:58:32.166 mobileCRM[3575:707] T restkit.support:RKCache.m:103 Wrote cached dictionary to cacheKey 'PermanentStore/37abc4aff62918578288d10530e6bcd6.headers'

When testing a change in the attribute "street" the changes are mapped well as well.

2012-04-11 15:58:33.013 mobileCRM[3575:1a03] T restkit.object_mapping:RKObjectMappingOperation.m:332 Mapped attribute value from keyPath 'street' to 'street'. Value: Musterweg 55

After finishing the operation I'm calling a new fetchRequest and reloading the belonging table view.

Problem

Somehow the changed object isn't saved in the backend (even with Core Data shows the COMMIT message) after the first call.

2012-04-11 16:05:44.235 mobileCRM[3603:351f] I restkit.core_data:RKInMemoryEntityCache.m:131 Caching all 2861 Company objectsIDs to thread local storage
2012-04-11 16:05:44.354 mobileCRM[3603:351f] CoreData: sql: BEGIN EXCLUSIVE
2012-04-11 16:05:44.357 mobileCRM[3603:351f] CoreData: sql: UPDATE ZCOMPANY SET ZSTREET = ?, Z_OPT = ?  WHERE Z_PK = ? AND Z_OPT = ?
2012-04-11 16:05:44.361 mobileCRM[3603:351f] CoreData: sql: COMMIT

But when I'm opening the app a second time to call the method again, the refreshed data is shown like expected. So I'm struggling to get the method working on the first call.

Thanks for your ideas!

Upvotes: 2

Views: 1281

Answers (1)

flashfabrixx
flashfabrixx

Reputation: 1183

After checking the output with

// App Delegate
RKLogConfigureByName("RestKit/*", RKLogLevelTrace);

// Scheme
-com.apple.CoreData.SQLDebug 1

I've realized that I reload the data before the commit of the data in Core Data is finished.

So resolve the problem by using the following method even when I'm using the RKRequestQueue.

- (void)objectLoaderDidFinishLoading:(RKObjectLoader *)objectLoader
{
    // Send notification to tableView
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshTableView" object:self];
}

... and it works. :)

Upvotes: 1

Related Questions