Realm數據庫的使用(二)數據庫的添加、刪除、修改、查詢


增加:

// 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對象。它包含了一系列的RLMObjectRLMResults有一個與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,RLMRealmRLMArray 和 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM