iCloud--Fetching Records

After you save records to the database, you can retrieve them using different mechanisms. Fetch individual records by record ID, or query for many records using a predicate. (A predicate defines logical conditions for searching for records.) Typically, you fetch a subset of records to display to the user at launch and then subscribe to changes that interest the user.
If you use the Location attribute type, you can also fetch records within a geographical region, as described in Fetching Records by Location .


1.Fetch Records by Identifier
If you know the record IDs for the records you want to fetch, then you can fetch by individual record ID. For example, this code fragment fetches a record named 115.如果你知道你想獲取記錄的記錄ID,那麼你可以通過單個記錄ID獲取。例如,下面的代碼片段獲取一個名爲115的記錄。

- (void)fetchRecordByIdentifier{

    // Fetch the record from the database

    CKDatabase *publicDatabase = [[CKContainer defaultContainer]publicCloudDatabase];

    CKRecordID *artworkRecordID = [[CKRecordID alloc]initWithRecordName:@"115"];

    [publicDatabase fetchRecordWithID:artworkRecordID completionHandler:^(CKRecord *record, NSError *error) {

        if (error) {

            NSLog(@"Error handling for failed fetch from public database");

        }else{

            NSLog(@"Display the fetched record");

        }

    }];

}


2.Fetch and Modify Records
You can fetch, modify, and save changes you make to individual records. This code fragment fetches an Artwork record, changes its date attribute value, and saves it to the database.

- (void)fetchRecordByIdentifier{

    // Fetch the record from the database

    CKDatabase *publicDatabase = [[CKContainer defaultContainer]publicCloudDatabase];

    

    CKRecordID *artworkRecordID = [[CKRecordID alloc]initWithRecordName:@"115"];

    [publicDatabase fetchRecordWithID:artworkRecordID completionHandler:^(CKRecord *record, NSError *error) {

        if (error) {

            NSLog(@"Error handling for failed fetch from public database");

        }else{

            NSLog(@"Display the fetched record");

            // Modify the record and save it to the database

            record[@"title"] = @"211";

            [publicDatabase saveRecord:record completionHandler:^(CKRecord *record, NSError *error) {

                if (error == nil) {

                    NSLog(@"modify");

                }

            }];

        }

    }];

}



3.Query for Records Using Predicates
If you have many records and store large files in iCloud, it is unlikely that you want to store all the records locally on a device. Instead you fetch a slice of the data using a query. A query combines a record type, a predicate, and a sort descriptor where the predicate contains attributes that are indexed. You build a query in code using a CKQuery object.如果你有很多的記錄和大文件要保存在iCloud中,你想要的所有記錄在本地存儲設備上是不現實的。相反,你按一些查詢條件獲取一些數據。查詢結合了記錄類型,謂詞和被索引的屬性。使用CKQuery對象代碼來創建一個查詢。
例如,下面的代碼片段獲取所有作品,其中美辰是藝術家。

For example, this code fragment fetches all artwork where Mei Chen is the artist.


CKDatabase *publicDatabase = [[CKContainer
containerWithIdentifier:containerIdentifier] publicCloudDatabase];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"artist = %@", @"Mei Chen"];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Artwork"
predicate:predicate];
[publicDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
if (error) {

// Error handling for failed fetch from public database
}
else {
// Display the fetched records
} }];


Recap
In this chapter you learned how to:
Fetch records by identifier
Fetch, modify, and save individual records
Fetch multiple records using a query and predicate













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章