CoreDataEnvir is a CoreData wrapper which use CoreData in convient way and supply thread safe in concurrenct programming. You can use it concurrenctly,run seperate CoreDataEnvir instance on one thread.
Register your data base file name
[CoreDataEnvir registDatabaseFileName:@"db.sqlite"];
Register your model file name(no file extension name)
[CoreDataEnvir registModelFileName:@"SampleModel"];
Let's assumption the Book
class is a model which represent a book object in your reading app. It has some field are name
, author
... etc.
There is an author "John Stevens Cabot Abbott" written a book named "Napoleon Bonnaparte".
[Book insertItemWithFillingBlock:^(id item) {
item.name = @"CoreData tutorial";
item.author = @"Headwindx";
}];
//Find all books of John Stevens Cabot Abbott.
NSArray *books= [Feed itemsWithFormat:@"author = %@", @"John Stevens Cabot Abbott"];
//Find one book model object.
Book *book = [Book lastItemWithFormat:@"name = %@ && author = %@", @"Napoleon Bonnaparte", @"John Stevens Cabot Abbott"];
[CoreDataEnvir asyncMainInBlock:^(CoreDataEnvir *db) {
[db deleteDataItem:book];
}];
[CoreDataEnvir asyncMainInBlock:^(CoreDataEnvir *db) {
[db deleteDataItemSet:books];
}];
You can do some lightweight operation on main thread. All of above operation must runs on main thread by default or it will raise an exception by CoreDataEnvir
. So you should be carefully.
It makes you feel more safe :-)
[CoreDataEnvir asyncMainInBlock:^(CoreDataEnvir *db) {
[Book insertItemWithFillingBlock:^(id item) {
item.name = @"CoreData tutorial";
item.author = @"Headwindx";
}];
}];
It's already prepared a background GCD queue for you in CoreDataEnvir
.
The block asyncBackgroundInBlock
will save memory cache to db file after void(^)(CoreDataEnvir *db)
works finished.
You don't need to use [db saveDataBase];
like older version.
[CoreDataEnvir asyncBackgroundInBlock:^(CoreDataEnvir *db) {
[Book insertItemOnBackgroundWithFillingBlock:^(id item) {
item.name = @"CoreData tutorial";
item.author = @"Headwindx";
}];
}];
It becomes more conveniently on concurrenct programming.
Must run on main queue:
+ (void)asyncMainInBlock:(void(^)(CoreDataEnvir *db))CoreDataBlock;
+ (id)insertItemWithFillingBlock:(void(^)(id item))fillingBlock;
+ (NSArray *)itemsWithFormat:(NSString *)fmt,...;
...
Must run on background queue, you can use these APIs and some methods name within Background
:
+ (void)asyncBackgroundInBlock:(void(^)(CoreDataEnvir *db))CoreDataBlock;
+ (id)insertItemOnBackgroundWithFillingBlock:(void(^)(id item))fillingBlock;
+ (NSArray *)itemsOnBackgroundWithFormat:(NSString *)fmt,...;
...
Or you wanna run some operation in your own dispatch queue, you can choose this APIs:
+ (CoreDataEnvir *)createInstance;
you'd better hold this instance for future.- (void)asyncInBlock:(void(^)(CoreDataEnvir *db))CoreDataBlock;
-
If you wanna keep you NSManagedObject objects, you shouldn't release you CoreDataEnvir object or it will be fault.
-
If you operate database in multiple threads, you should make sure you have one NSManagedObjectContext in seperate thread or GCD queue. Or it will occur conflict while one thread reading data and other one thread is writing data.
PS: So, in CoreDataEnvir please make sure your NSManagedObject object reference fetched from [CoreDataEnvir mainInstance] on main thread or from [CoreDataEnvir backgroundInstance] on background thread supplied by CoreDataEnvir and which never be released until application exist and it's enough for usual.