Personというエンティティ名をサンプルとして使用する。
こんな感じのデータ構造になっております。
Person
--name
--age
Insert
[MagicalRecord setupCoreDataStack]; Person *person = [Person MR_createEntity]; person.name = @"Sato Taro"; person.age = [NSNumber numberWithInt: 19];これだけで新しいPersonを作成することが出来る。
Select
[MagicalRecord setupCoreDataStack]; NSArray *persons = [Person MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(name == '%@')", @"Sato Taro"]];これが単純なセレクト
ANDでつなぐ場合
[MagicalRecord setupCoreDataStack]; Person *person = [Person MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(name == 'Sato Taro') AND (age == 19)"]];SORTしたいとき
[MagicalRecord setupCoreDataStack]; NSArray *persons = [Persn MR_findAllSortedBy:@"age" ascending:YES];ほかにもっと複雑なセレクトを書けたいときは順次加えていく。
Update
[MagicalRecord setupCoreDataStack]; Person *person = [Person MR_findFirstWithPredicate: [NSPredicatepredicateWithFormat:@"name = 'Sato Taro'"]]; person.name = @"Sato Kogoro";これだけ!
Delete
[MagicalRecord setupCoreDataStack]; Person *person = [Person MR_findFirstWithPredicate: [NSPredicatepredicateWithFormat:@"name = 'Sato Taro'"]]; [person MR_deleteEntity];
今のところこんな感じです。新しいことがわかったら随時加えていきます。
No comments:
Post a Comment