CloudKit:获取具有特定记录类型的所有记录? [英] CloudKit: Fetch all records with a certain record type?
问题描述
我目前在我的应用程序中设置了 CloudKit ,以便我可以使用下面的以下代码来添加新记录:
I have currently got CloudKit set up in my app so that I am adding a new record using the help of the following code below,
CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:@"stringArray"];
CKRecord *record = [[CKRecord alloc] initWithRecordType:@"Strings" recordID:recordID];
[record setObject:[NSArray arrayWithObjects:@"one", @"two", @"three", @"four", nil] forKey:@"stringArray"];
[_privateDatabase saveRecord:record completionHandler:nil];
但是,现在我希望能够获取所有相同记录类型的记录, 字符串,并返回那些编译为NSArray的字符串。我将如何去做?目前,我所能想到的就是如何使用recordID单独获取每条记录,这很麻烦,必须有一种更简单的方法。
However, now I would like to be able to fetch ALL records that are of the same record type, "Strings," and return those compiled into an NSArray. How would I go about doing that? Currently, all I have figured out is how to fetch each record individually, using a recordID, which is a hassle, there must be an easier way.
[_privateDatabase fetchRecordWithID:recordID completionHandler:^(CKRecord *record, NSError *error) {
if (error) {
// Error handling for failed fetch from private database
}
else {
NSLog(@"ICLOUD TEST: %@", [record objectForKey:@"stringArray"]);
}
}];
推荐答案
Aaaand,我明白了。使用下面的代码,我能够创建一个查询来在数据库上运行,然后在完成的块中返回一个NSArray,并循环遍历它,并在NSLog中返回已保存键的值。
Aaaand, I've got it. Using the code below, I was able to create a query to run on the database, to then return an NSArray in the completion block, which I looped through, and returned the value for the saved key in an NSLog.
NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Strings" predicate:predicate];
[_privateDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
for (CKRecord *record in results) {
NSLog(@"Contents: %@", [record objectForKey:@"stringArray"]);
}
}];
这篇关于CloudKit:获取具有特定记录类型的所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!