Cra*_*tis 13 macos objective-c ios icloud
我在我的应用程序中有基本的iCloud支持(同步更改,无处不在等),但到目前为止一个重要的遗漏是缺乏对云中存在(或有变化)的文件的"下载"支持,但不是与当前磁盘上的内容同步.
我根据一些Apple提供的代码,在我的应用程序中添加了以下方法,并进行了一些调整:
下载方式:
- (BOOL)downloadFileIfNotAvailable:(NSURL*)file {
NSNumber* isIniCloud = nil;
if ([file getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil]) {
// If the item is in iCloud, see if it is downloaded.
if ([isIniCloud boolValue]) {
NSNumber* isDownloaded = nil;
if ([file getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) {
if ([isDownloaded boolValue])
return YES;
// Download the file.
NSFileManager* fm = [NSFileManager defaultManager];
NSError *downloadError = nil;
[fm startDownloadingUbiquitousItemAtURL:file error:&downloadError];
if (downloadError) {
NSLog(@"Error occurred starting download: %@", downloadError);
}
return NO;
}
}
}
// Return YES as long as an explicit download was not started.
return YES;
}
- (void)waitForDownloadThenLoad:(NSURL *)file {
NSLog(@"Waiting for file to download...");
id<ApplicationDelegate> appDelegate = [DataLoader applicationDelegate];
while (true) {
NSDictionary *fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:[file path] error:nil];
NSNumber *size = [fileAttribs objectForKey:NSFileSize];
[NSThread sleepForTimeInterval:0.1];
NSNumber* isDownloading = nil;
if ([file getResourceValue:&isDownloading forKey:NSURLUbiquitousItemIsDownloadingKey error:nil]) {
NSLog(@"iCloud download is moving: %d, size is %@", [isDownloading boolValue], size);
}
NSNumber* isDownloaded = nil;
if ([file getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) {
NSLog(@"iCloud download has finished: %d", [isDownloaded boolValue]);
if ([isDownloaded boolValue]) {
[self dispatchLoadToAppDelegate:file];
return;
}
}
NSNumber *downloadPercentage = nil;
if ([file getResourceValue:&downloadPercentage forKey:NSURLUbiquitousItemPercentDownloadedKey error:nil]) {
double percentage = [downloadPercentage doubleValue];
NSLog(@"Download percentage is %f", percentage);
[appDelegate updateLoadingStatusString:[NSString stringWithFormat:@"Downloading from iCloud (%2.2f%%)", percentage]];
}
}
}
Run Code Online (Sandbox Code Playgroud)
以及启动/检查下载的代码:
if ([self downloadFileIfNotAvailable:urlToUse]) {
// The file is already available. Load.
[self dispatchLoadToAppDelegate:[urlToUse autorelease]];
} else {
// The file is downloading. Wait for it.
[self performSelector:@selector(waitForDownloadThenLoad:) withObject:[urlToUse autorelease] afterDelay:0];
}
Run Code Online (Sandbox Code Playgroud)
据我所知,上面的代码看起来很好,但是当我在Device A上进行大量更改时,保存这些更改,然后打开Device B(提示在Device B上下载)这就是我在控制台中看到的:
2012-03-18 12:45:55.858 MyApp[12363:707] Waiting for file to download...
2012-03-18 12:45:58.041 MyApp[12363:707] iCloud download is moving: 0, size is 101575
2012-03-18 12:45:58.041 MyApp[12363:707] iCloud download has finished: 0
2012-03-18 12:45:58.041 MyApp[12363:707] Download percentage is 0.000000
2012-03-18 12:45:58.143 MyApp[12363:707] iCloud download is moving: 0, size is 101575
2012-03-18 12:45:58.143 MyApp[12363:707] iCloud download has finished: 0
2012-03-18 12:45:58.144 MyApp[12363:707] Download percentage is 0.000000
2012-03-18 12:45:58.246 MyApp[12363:707] iCloud download is moving: 0, size is 101575
2012-03-18 12:45:58.246 MyApp[12363:707] iCloud download has finished: 0
2012-03-18 12:45:58.246 MyApp[12363:707] Download percentage is 0.000000
2012-03-18 12:45:58.347 MyApp[12363:707] iCloud download is moving: 0, size is 177127
2012-03-18 12:45:58.347 MyApp[12363:707] iCloud download has finished: 0
2012-03-18 12:45:58.347 MyApp[12363:707] Download percentage is 0.000000
2012-03-18 12:45:58.449 MyApp[12363:707] iCloud download is moving: 0, size is 177127
2012-03-18 12:45:58.449 MyApp[12363:707] iCloud download has finished: 0
2012-03-18 12:45:58.450 MyApp[12363:707] Download percentage is 0.000000
Run Code Online (Sandbox Code Playgroud)
无论出于何种原因:
我究竟做错了什么?
MrG*_*mez 11
似乎添加一个延迟有助于缓解未知的竞争条件,但尚未知道已知的解决方法.从3月21日:
无论如何,我想知道你是否曾经过这篇文章中的主要问题?也就是说,随着时间的推移,同步似乎会降低,导入通知会停止到达或不完整.您已经确定在响应导入通知时添加延迟具有一定的价值,但最终证明是不可靠的.
并从链接文章的OP:
我遇到的问题似乎是由竞争条件引起的.有时我会收到通知我的持久存储已从iCloud更新 - 但更新的信息尚不可用.这种情况似乎在没有延迟的情况下发生在大约1/4的时间,并且在延迟的大约1/12的时间发生.
它不像稳定性降级......系统将在下次启动应用程序时始终捕获更新,并且自动冲突解决方案解决了问题.然后它将继续正常运行.但是,最终,它将放弃另一次更新.
...
在某种程度上,我认为我们只需要相信iCloud最终会推出信息,而我们的冲突解决代码(或核心数据的自动冲突解决方案)将解决出现的任何问题.
不过,我希望Apple修复竞争条件错误.这不应该发生.
因此,在撰写本文时,使用此API的iCloud下载应该被视为不可靠.
(附加参考)
Cra*_*tis 10
多年以后,我仍然经历下载文件的问题(虽然在其他答案中的一些解决方法之后更加罕见).所以,我联系了Apple的开发人员,要求进行技术审查/讨论,这就是我发现的内容.
定期检查相同的下载状态NSURL,即使您正在重新创建它,也不是检查状态的首选方法.我不知道它为什么不是 - 它似乎应该有用,但事实并非如此.相反,一旦开始下载文件,您应该注册一个观察者以NSNotificationCenter获取该下载的进度,并保留对该文件的查询的引用.这是提供给我的确切代码示例.我在我的应用程序中实现了它(通过一些特定于应用程序的调整),它似乎表现得更加恰当.
- (void)download:(NSURL *)url
{
dispatch_queue_t q_default;
q_default = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(q_default, ^{
NSError *error = nil;
BOOL success = [[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:url error:&error];
if (!success)
{
// failed to download
}
else
{
NSDictionary *attrs = [url resourceValuesForKeys:@[NSURLUbiquitousItemIsDownloadedKey] error:&error];
if (attrs != nil)
{
if ([[attrs objectForKey:NSURLUbiquitousItemIsDownloadedKey] boolValue])
{
// already downloaded
}
else
{
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[query setPredicate:[NSPredicate predicateWithFormat:@"%K > 0", NSMetadataUbiquitousItemPercentDownloadedKey]];
[query setSearchScopes:@[url]]; // scope the search only on this item
[query setValueListAttributes:@[NSMetadataUbiquitousItemPercentDownloadedKey, NSMetadataUbiquitousItemIsDownloadedKey]];
_fileDownloadMonitorQuery = query;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(liveUpdate:)
name:NSMetadataQueryDidUpdateNotification
object:query];
[self.fileDownloadMonitorQuery startQuery];
}
}
}
});
}
- (void)liveUpdate:(NSNotification *)notification
{
NSMetadataQuery *query = [notification object];
if (query != self.fileDownloadMonitorQuery)
return; // it's not our query
if ([self.fileDownloadMonitorQuery resultCount] == 0)
return; // no items found
NSMetadataItem *item = [self.fileDownloadMonitorQuery resultAtIndex:0];
double progress = [[item valueForAttribute:NSMetadataUbiquitousItemPercentDownloadedKey] doubleValue];
NSLog(@"download progress = %f", progress);
// report download progress somehow..
if ([[item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey] boolValue])
{
// finished downloading, stop the query
[query stopQuery];
_fileDownloadMonitorQuery = nil;
}
}
Run Code Online (Sandbox Code Playgroud)
最近我遇到了同样的情况 - 就像上面一样,我看到下载明显发生了,但是NSURLUbiquitousItemIsDownloading,所有其他键,总是返回false.一位同事提到,iCloud工程师建议创建一个新的NSURL来检查这些NSURLUbiquitousItem密钥,因为元数据在创建后可能不会(并且显然不会)更新.我在检查之前创建了一个新的NSURL,它确实反映了当前的状态.
不要忽视@MrGomez提到的竞争条件,这是一个重大问题(特别是在iOS 5中普遍存在,并且引起了许多麻烦),但我不相信解释上述问题.
编辑:为了创建NSURL我使用的新[NSURL fileURLWithPath:originalURL.path].虽然有点凌乱,但它是第一件可靠的工作.我刚试过[originalURL copy]并再次使用旧的元数据,所以显然它也被复制了.
为了安全起见,或者直到进一步记录,我计划假设除非NSURL在任何getResourceValue:forKey:调用之前创建一个新的,否则将返回过时的元数据.
| 归档时间: |
|
| 查看次数: |
9446 次 |
| 最近记录: |