CoreDataEnvir

CoreDataEnvirSample 主页

CoreDataEnvir (2011-05-25)

A CoreData Envirement wrapper, use CoreData in convient way. You can use it in concurrency,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 extension)
[CoreDataEnvir registModelFileName:@"SampleModel"];

On main thread:

Insert item:

Team *team = [Team insertItem];
or
[Team insertItemWith:^(Team *item) {
    item.name = [NSString stringWithFormat:@"Nicholas"];
}];

[[CoreDataEnvir instance] saveDataBase];

Fetch one item:

Team *team = (Team *)[Team lastItemWith:[NSPredicate predicateWithFormat:@"name==nicholas"]];

Fetch all items:

NSArray *items = [Team itemsWith:[NSPredicate predicateWithFormat:@"name==nicholas"]];

If you want to run on other threads, follow this:

This method ([CoreDataEnvir instance]) will automatically creating new instance for non-main thread and uniq instance for main thread.

dispatch_async(q2, ^{
    CoreDataEnvir *db = [CoreDataEnvir instance];
    for (int i = 0; i < 500; i++) {
        Team *team = (Team *)[Team lastItemWith:db predicate:[NSPredicate predicateWithFormat:@"name==9"]];
        if (team) {
            [db deleteDataItem:team];
        }else {
            [Team insertItemWith:db fillData:^(Team *item) {
                item.name = [NSString stringWithFormat:@"9"];
            }];

        }
        [db saveDataBase];
    }
});

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This page loaded in 0.235 seconds with 46 database queries.