LFSCoreData is an iOS and MacOSX open-source library to help the developer to start Core Data Framework based applications easily. Manage your application database bootstraping, saving in background and mapping from an API easily.
To run the example project; clone the repo, and run pod install
from the Project directory first.
- Easy setup and clean AppDelegate
- Easily import API data from JSON data
- Custom field naming
- Save in main context without affecting UI
- Save in background in a NSOperationQueue
Podfile
platform :ios, '7.0'
pod 'LFSCoreData', '~> 1.0'
Before starting using LFSCoreData you should read this patterns, and create your entities in xcdatamodel:
Setup your project using LFCoreDataPatterns
NSDictionary *object = @{ name: @"Son", surname: @"Goku" };
User *user = [User importObject:object inContext:[[LFDataModel sharedModel] mainContext]];
// Save context to be persistent
[[LFDataModel sharedModel] saveContext];
...
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[Tweet importFromArray:JSON[@"results"] inContext:[[LFDataModel sharedModel]mainContext]];
// Save context to be persistent
[[LFDataModel sharedModel] saveContext];
[self updateTweetsInUI];
} failure:nil];
...
...
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[LFSaveInBackgroundOperation saveInBackgroundWithBlock:^(NSManagedObjectContext *backgroundContext) {
[Tweet importFromArray:JSON[@"results"] inContext:backgroundContext];
//Save in background already executes save method, so here is not necessary
} completion:^{
[self updateTweetsInUI];
}];
[self updateTweetsInUI];
} failure:nil];
…
...
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[LFSaveInBackgroundOperation saveInBackgroundWithBlock:^(NSManagedObjectContext *backgroundContext) {
Tweet *tweet = [Event objectForIdentifier:JSON[@"id"]
inManagedObjectContext:backgroundContext
forEntityName:[Tweet entityName]];
if (!tweet){
tweet = [Tweet insertInManagedObjectContext:backgroundContext];
}
tweet.tweetID = JSON[@"id"];
tweet.name = [JSON[@"name"] uppercaseString];
} completion:^{
[self updateTweetsInUI];
}];
[self updateTweetsInUI];
} failure:nil];
…
This way you can delete all the elements in the database of the importing entity, except the ones you are importing now.
Can be useful when using LFSCoreData as a cache of your data and this data is volatile.
The usage is like this:
[Tweet importFromArray:yourArrayOfNewObjects
inContext:[[LFSDataModel sharedModel] mainContext]
deleteOtherObjects:YES];
Now you can delete all objects for an specific entity like this:
[Tweet deleteAllObjects];
LAFOSCA STUDIO S.L.
LFSCoreData is available under the MIT license. See the LICENSE file for more info.