GithubHelp home page GithubHelp logo

liuson / coredataenvir Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dehengxu/coredataenvir

0.0 2.0 0.0 1.06 MB

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

Home Page: http://xudeheng.github.io/CoreDataEnvir

License: MIT License

Ruby 5.47% Objective-C 75.14% JavaScript 4.43% CSS 0.73% HTML 13.31% Shell 0.92%

coredataenvir's Introduction

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 registDatabaseFileName:@"db.sqlite"];

Register your model file name(no file extension name)

	[CoreDataEnvir registModelFileName:@"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

coredataenvir's People

Contributors

bitdeli-chef avatar dehengxu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.