简体   繁体   中英

What are keys and how do I use them?

I'm working on an iCloud compatible app and I am researching how to detect whether a file is being uploaded/downloaded or has completed that. I found that this can be detected with NSURL "keys," such as NSURLUbiquitousItemIsDownloadingKey or NSURLUbiquitousItemIsUploadingKey . I'm still working on learning about programming, so what are these keys? How can I use them to detect the status of the files (I want the app to know when a file is done uploading to iCloud or done downloading (whichever side the device is on)).

I read that I can use resourceValuesForKeys:error: to query the state of these keys, so would I put this into an IF statement and see if the result is expected, such as "yes" or "no"? Thanks for your help.

if ([destination resourceValuesForKeys:[NSArray arrayWithObject:NSURLUbiquitousItemIsUploadingKey] error:NULL]) {

    //is uploading??

}

Your proposed code looks almost workable, but for one thing: resourceValuesForKeys:error: returns a dictionary whose keys are the same as the constants you pass in and whose values are as specified in the documentation for those keys . In the case of NSURLUbiquitousItemIsUploadingKey , the value is an NSNumber instance wrapping a BOOL value.

So... assuming destination is an NSURL pointing to an item in your ubiquity container:

NSError *error;
NSArray *keys = [NSArray arrayWithObject:NSURLUbiquitousItemIsUploadingKey];
NSDictionary *values = [destination resourceValuesForKeys:keys error:&error];
if (values == nil)
    NSLog(@"error: %@", error);
else if ([[values objectForKey:NSURLUbiquitousItemIsUploadingKey] boolValue])
    NSLog(@"uploading");
else
    NSLog(@"not uploading");

If you're only querying one key, you can use getResourceValue:forKey:error: to be a little more concise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM