Reputation: 1263
I have a project with sqlite database and work with it via MagicalRecord library:
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:storeFileName];
In current version of app i work with web-service (RPC) via Transient objects: send requests, map response to objects and show it. In database i save only which items user added to favorites.
Now i want implement API calls with AFIncrementalStore for simple caching and offline work of app. First question how can i add AFIncrementalStore in existed CoreData stack? I am trying to add store in memory for example:
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_defaultStoreCoordinator];
AFIncrementalStore *incrementalStore = (AFIncrementalStore *)[coordinator addPersistentStoreWithType:[RPCAPIIncrementalStore type] configuration:nil URL:nil options:nil error:nil];
NSError *error = nil;
if (![incrementalStore.backingPersistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
But when i trying to fetch entities from my local database (i don't need api call for that entities) my api incremental store handle that request and trying to get this item from server!
How should i properly configure my stack for achieve efficient work with objects in remote database and local? Is this possible for AFIncrementalStore?
Upvotes: 0
Views: 189
Reputation: 1624
Here is the code I use to set up the AFIncrementalStore. Should be something similar for your setup:
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"ShuffleModel.sqlite"];
AFIncrementalStore *incrementalStore = (AFIncrementalStore *)[[NSPersistentStoreCoordinator MR_defaultStoreCoordinator] addPersistentStoreWithType:[SFIncrementalStore type] configuration:nil URL:nil options:nil error:nil];
NSError *error = nil;
if (![incrementalStore.backingPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSPersistentStore MR_defaultLocalStoreUrl] options:nil error:&error]){
DDLogVerbose(@"Unresolved NSIncrementalStore error %@, %@", error, [error userInfo]);
abort();
}
Not sure if it helps you or not, but I was able to get this to work by hacking it with the NSMainQueueConcurrency thing. It's migrated to AFNetworking 2.0 and seems to work ok. Here is the code: https://github.com/premosystems/AFIncrementalStore
See ths issue here: https://github.com/AFNetworking/AFIncrementalStore/issues/265
Upvotes: 0