TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | May 2016 |
Maintained by Luciano Bastos Nunes.
LBNCoreDataStack is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "LBNCoreDataStack"
iOS 7.1 or greater.
You can remove everything related to CoreData that were put in your AppDelegate.m and AppDelegate.h.
In AppDelegate.m it will be after the line:
#pragma mark - Core Data stack
and this line:
#pragma mark - Core Data Saving support
From AppDelegate.h you remove the lines:
#import <CoreData/CoreData.h>
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
Those lines of code will be now handled by LBNCoreDataStack you don't need to be concerned about then anymore.
To use LBNCoreDataStack you have to include it in the class file you will be using it.
#import "LBNCoreDataStack.h"
Then you will be needing to cofigure your CoreDataStack with the name of your Core Data object. It is usualy the name of your project so if your project is named MyBigProject you can use:
[[LBNCoreDataStack defaultStack] setResourceName:@"MyBigProject"];
It is recommended to do this in your AppDelegate class.
OK, thats pretty much all you have to do with LBNCoreDataStack to start using it.
To facilitate its usage you can use the CoreData with the methods from LBNCoreDataStack as you would from AppDelegate before, or you can use LBNPersistence class with its facilitators methods that include easy ways to include, exclude and modify your Core Data Entity content.
To use it, first import the header,
#import "LBNPersistence.h"
instantiate the class,
LBNPersistence = [[LBNPersistence alloc] init];
and use those methods:
- (void)removeObject:(id)object
This method will delete an object of your Entity and will save your context to disk.
- (void)clearEntity:(NSString *)entity Save:(BOOL)save
This method will delete all objects from a given Entity.
- (void)clearDB:(void (^)(void))block
Call this method to clear all Enties in your CoreData.
- (NSArray *)parseJSON:(id)json ForTags:(NSArray *)tags Error:(NSError **)error
This method will parse a given JSON formatted as a NSArray of NSDictionary. It get the json and parse in acordanse to the tags rules and return the NSArray formatted to be included in an Entity.
@{@"name":@"title",
@"type":[NSNumber numberWithUnsignedInteger:DMString],
@"operation":self.plainText,
@"toName":@"title"}
- (NSArray *)insertDataCollection:(NSArray *)collection CheckAttibutes:(NSArray *)attributes InEntity:(NSString *)entityName Saving:(BOOL)toSave
This will get a array of dictionary formatted with your keys having the same name as your Entity attibute and checking for the attributes to know if you have the same object already.
- (NSArray *)fetchWithPredicate:(NSPredicate *)predicate EntityName:(NSString *)entityName SortDescriptors:(NSArray *)sortDescriptors;
This method is used to get an instance of desired entity. You can use predicate and sortDescriptor to filter and sort your fetch, or pass nil to those parameters to fetch all objects in an Entity.
- (bool)isDBEmpty;
Check all CoreData Entities for object.
If there is no object in DB return YES else return NO.
- (void)save;
Same as commit on Data Base. It will save on disk all changes you made on memory.
Luciano Bastos Nunes, [email protected]
LBNCoreDataStack is available under the MIT license. See the LICENSE file for more info.