Skip to content

A CoreData Envirement wrapper, use CoreData in convenient way and it's thread safe. It has been used in my works.

License

Notifications You must be signed in to change notification settings

dehengxu/CoreDataEnvir

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CoreDataEnvir (since 2011-05-25) latest version 0.4

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.

First step:

Register your data base file name

	[CoreDataEnvir registerDatabaseFileName:@"db.sqlite"];

Register your model file name(no file extension name)

	[CoreDataEnvir registerModelFileName:@"SampleModel"];

Simple data base access:

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".

Add new record

[Book insertItemWithFillingBlock:^(id item) {
	item.name = @"CoreData tutorial";
	item.author = @"Headwindx";
}];

Fetch lots of records

//Find all books of John Stevens Cabot Abbott.
NSArray *books= [Feed itemsWithFormat:@"author = %@",  @"John Stevens Cabot Abbott"];

Fetch one record

//Find one book model object.
Book *book = [Book lastItemWithFormat:@"name = %@ && author = %@", @"Napoleon Bonnaparte", @"John Stevens Cabot Abbott"];

Delete one record

[CoreDataEnvir asyncMainInBlock:^(CoreDataEnvir *db) {
		[db deleteDataItem:book];
}];

Delete records

[CoreDataEnvir asyncMainInBlock:^(CoreDataEnvir *db) {
		[db deleteDataItemSet:books];
}];

Concurrenct programming:

On main thread

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.

You also can explicit use on main thread

It makes you feel more safe :-)

[CoreDataEnvir asyncMainInBlock:^(CoreDataEnvir *db) {
	[Book insertItemWithFillingBlock:^(id item) {
		item.name = @"CoreData tutorial";
		item.author = @"Headwindx";
	}];
}];

On background thread

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.

Convenient methods

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 your are newcomer to CoreData, please obey the rules below:

  • 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.

Bitdeli Badge

About

A CoreData Envirement wrapper, use CoreData in convenient way and it's thread safe. It has been used in my works.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published