VDUBStep is a lightweight framework that wraps Core Data's NSManagedObjectModel, NSPersistentStoreCoordinator and NSManagedObjectContext into a utility class, VDUBStore.
Set up in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [documentsURL URLByAppendingPathComponent:@"vdub.sqlite"];
[[VDUBStore sharedInstance] setupCoreDataStackWithURL:storeURL];
return YES;
}
The framework also provides convenience methods for NSManagedObject and NSManagedObjectContext.
Create an instance of MyManagedObject:
MyManagedObject *mmo = [MyManagedObject createInContext:[VDUBStore mainContext]]]];
mmo.uniqueID = @1;
mmo.name = @"A Name";
[[VDUBStore mainContext] save];
Get a MyManagedObject by ID:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"uniqueID = %d", 1];
MyManagedObject *mmo = [MyManagedObject findFirstWithPredicate:predicate inContext:[VDUBStore mainContext]];
mmo.name = @"Another name";
[[VDUBStore mainContext] save];
Get array of all MyManagedObjects sorted by name:
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES];
NSArray *mmos = [MyManagedObject findAllWithSortDescriptors:@[ sortDescriptor ]
inContext:[VDUBStore mainContext]];
Delete an instance of MyManagedObject:
[mmo delete];
[[VDUBStore mainContext] save];
It is currently assumed that your NSManagedObjects implement entityName, such as is provided when they are generated by mogenerator.