标签:
// Create object Person *author = [[Person alloc] init]; author.name = @"David Foster Wallace"; // Get the default Realm RLMRealm *realm = [RLMRealm defaultRealm]; // You only need to do this once (per thread) // Add to Realm with transaction [realm beginWriteTransaction]; [realm addObject:author]; [realm commitWriteTransaction];
Book *cheeseBook = ... // Book stored in Realm // Delete an object with a transaction [realm beginWriteTransaction]; [realm deleteObject:cheeseBook]; [realm commitWriteTransaction]; // Delete all objects from the realm [realm beginWriteTransaction]; [realm deleteAllObjects]; [realm commitWriteTransaction];
// Update an object with a transaction [realm beginWriteTransaction]; author.name = @"Thomas Pynchon"; [realm commitWriteTransaction]; // Creating a book with the same primary key as a previously saved book Book *cheeseBook = [[Book alloc] init]; cheeseBook.title = @"Cheese recipes"; cheeseBook.price = @9000; cheeseBook.id = @1; // Updating book with id = 1 [realm beginWriteTransaction]; [Book createOrUpdateInRealm:realm withValue:cheeseBook]; [realm commitWriteTransaction];
Realm的对象查询返回一个RLMResults对象。它包含了一系列的RLMObject。RLMResults有一个与NSArray很相似的interface(接口)并且对象可以通过索引(index)下标获取。 但不同于NSArrays的是,RLMResult是归类的——它只能容纳一种RLMObjects类型
从realm中获取对象的最基本方法就是 [RLMObject allObjects]
, 它返回一个RLMResults,里面是查询的子类的所有RLMObject实例。
// Query the default Realm RLMResults *dogs = [Dog allObjects]; // retrieves all Dogs from the default Realm // Query a specific Realm RLMRealm *petsRealm = [RLMRealm realmWithPath:@"pets.realm"]; // get a specific Realm RLMResults *otherDogs = [Dog allObjectsInRealm:petsRealm]; // retrieve all Dogs from that Realm
如果你对 NSPredicate很熟悉的话, 那么你就已经知道怎么在realm里面查询了。RLMObjects,RLMRealm, RLMArray 和 RLMResults都提供很好的方法来查询特定的RLMObjects:只需要传递相应地NSPredicate实例,谓词字符串,谓词格式字符串,就可以获取你想要的RLMObjects实例啦。就和NSObject一样。
举个例子,下面的代码就是对上面的拓展。 通过调用[RLMObject objectsWhere:], 获得了默认realm数据库中的所有颜色是黄褐色的,名字开头是“B”的狗的实例。
// Query using a predicate string RLMResults *tanDogs = [Dog objectsWhere:@"color = ‘tan‘ AND name BEGINSWITH ‘B‘"]; // Query using an NSPredicate object NSPredicate *pred = [NSPredicate predicateWithFormat:@"color = %@ AND name BEGINSWITH %@", @"tan", @"B"]; tanDogs = [Dog objectsWithPredicate:pred];
可以参看Apple的Predicates Programming Guide 了解更多关于如何创建谓词
在很多情况下,我们都希望获取或者查询返回的结果都能按照一定条件排序。所以,RLMArray支持使用指定的属性对数据列进行排序。Realm允许指定一个排序要求并且根据一个或多个属性进行排序。举例来说, 下面代码调用了[RLMObject objectsWhere:where:]
对返回的数据”dogs”进行排序,排序的条件是名字的字母表升序:
// Sort tan dogs with names starting with "B" by name RLMResults *sortedDogs = [[Dog objectsWhere:@"color = ‘tan‘ AND name BEGINSWITH ‘B‘"] sortedResultsUsingProperty:@"name" ascending:YES];
Realm查询引擎的一个独特属性就是它能够进行简单快捷的链式查询, 而不需要像传统数据库一样的麻烦。
举个例子来说,假如你要所有黄褐色的小狗的结果序列,然后从中查找名字开头是“B“的小狗。 你可以发送如下的请求。
RLMResults *tanDogs = [Dog objectsWhere:@"color = ‘tan‘"]; RLMResults *tanDogsWithBNames = [tanDogs objectsWhere:@"name BEGINSWITH ‘B‘"];
更多内容请参看官网:Realm-Objective-C
标签:
原文地址:http://www.cnblogs.com/Erickson/p/Realm-change.html