无法从第一台设备上写入的第二台设备读取iCloud文件

时间:2012-11-21 22:36:52

标签: ios icloud

我无法在一台设备上从我的应用程序向iCloud容器写一个简单文件,然后成功读取同一文件第一次我的应用程序在第二台设备上运行。

我在一对刚刚重置的设备上使用空的Ubiquity容器启动了这个测试场景。下面是一个测试用例代码和控制台日志记录,演示了我遇到的问题。下面的所有代码都在后台线程上运行。

我在第一台设备上安装并运行测试用例。代码在ubiquity容器中查找某个文件,并在找不到它时创建它。显然,这是第一次在第一台设备上运行时。我希望(并且期待)当应用程序刚刚安装并在第二个设备上运行时,它将能够找到并读取在第一个设备上创建的文件的内容。似乎重置设备上新安装的应用程序永远无法查找和读取现有文件,并且始终认为需要创建一个新文件,不幸的是,如果已经存在,最终将替换无处不在容器中的文件。

然而,在第一次尝试(在第二台设备上)无法找到并读取文件后,如果我删除了应用程序,重新安装然后在同一设备上重新运行它,它确实找到了该文件并且是能够阅读其当前内容。看起来应用程序的第一次运行会导致文件被加载到普遍存在的缓存中,但不会处于应用程序可以找到的状态。应用程序的第二次运行似乎在缓存中找到该文件。

我知道您应该开始在iOS设备上下载文件,但是第一次使用设备时,它会报告“找不到文件”。还有什么需要在这里完成的吗?或者这看起来像一个新的或已知的错误?

以下代码用于确定ubiquity容器中是否存在令牌文件,并确保可以读取它。

NSError *error;
BOOL rc;

NSFileManager *fm = [[NSFileManager alloc] init];
NSURL *ubiquityURL = [fm URLForUbiquityContainerIdentifier:nil];
NSURL *tokenURL = [ubiquityURL URLByAppendingPathComponent:@"iCloudTokenFile"];

NSLog(@"isTokenFilePresentAtUrl: Entered for Device Name: %@", [[UIDevice currentDevice] name]);

error = nil;
rc = [fm startDownloadingUbiquitousItemAtURL:tokenURL error:&error];
NSLog(@"startDownloadingUbiquitousItemAtURL: %d Error: %@(%d)", rc, error.domain, error.code);

rc = [fm isUbiquitousItemAtURL:tokenURL];
NSLog(@"isUbiquitousItemAtURL: %d", rc);

NSNumber *isUbiquitousItem = nil;
error = nil;
rc = [tokenURL getResourceValue:&isUbiquitousItem forKey:NSURLIsUbiquitousItemKey error:&error];
NSLog(@"getResourceValue: %d isUbiquitousItem: %@ Error: %@(%d)", rc, isUbiquitousItem, error.domain, error.code);

NSNumber *isDownloading = nil;
error = nil;
rc = [tokenURL getResourceValue:&isDownloading forKey:NSURLUbiquitousItemIsDownloadingKey error:&error];
NSLog(@"getResourceValue: %d isDownloading: %@ Error: %@(%d)", rc, isDownloading, error.domain, error.code);

NSNumber *isDownloaded = nil;
error = nil;
rc = [tokenURL getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:&error];
NSLog(@"getResourceValue: %d isDownloaded: %@ Error: %@(%d)", rc, isDownloaded, error.domain, error.code);

以下代码用于尝试从ubiquity容器中读取令牌文件。

__block BOOL success = NO;
__block NSError *error = nil;

NSFileCoordinator *fc = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fc coordinateReadingItemAtURL:tokenURL
                       options:NSFileCoordinatorReadingWithoutChanges
                         error:&error
                    byAccessor:^(NSURL *newURL)
 {
     NSLog(@"readTokenFileFromUrl: Trying to read Token File.");

     NSNumber *isDownloaded = nil;
     error = nil;
     BOOL rc = [tokenURL getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:&error];
     NSLog(@"getResourceValue: %d isDownloaded: %@ Error: %@(%d)", rc, isDownloaded, error.domain, error.code);

     NSString *uuidString =  nil;
     NSError *blockerror = nil;
     uuidString = [NSString stringWithContentsOfURL:newURL
                                           encoding:NSUTF8StringEncoding
                                              error:&blockerror];

     NSLog(@"readTokenFileFromUrl: Token read from file: %@", uuidString);
 }];

如果无法读取现有令牌文件,则使用以下代码将令牌文件写入ubiquity容器。我原以为这个代码只能由第一个设备运行一次。

NSFileCoordinator *fc = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSError *error = nil;
[fc coordinateWritingItemAtURL:tokenURL
                       options:NSFileCoordinatorWritingForReplacing
                         error:&error
                    byAccessor:^(NSURL *newURL)
 {
     NSUUID *uuid = [[NSUUID alloc] init];
     NSString *uuidString = [uuid UUIDString];

     NSError *blockerror = nil;
     NSLog(@"writeTokenFileToUrl: Writing token to file: %@", uuidString);

     [uuidString writeToURL:newURL
                 atomically:YES
                   encoding:NSUTF8StringEncoding
                      error:&blockerror];
 }];

该应用程序首次在“Red iPod touch”上安装并运行。

isTokenFilePresentAtUrl: Entered for Device Name: Red iPod touch 
startDownloadingUbiquitousItemAtURL: 0 Error: NSCocoaErrorDomain(4) 
isUbiquitousItemAtURL: 0 
getResourceValue: 0 isUbiquitousItem: (null) Error: NSPOSIXErrorDomain(2) 
getResourceValue: 0 isDownloading: (null) Error: NSPOSIXErrorDomain(2) 
getResourceValue: 0 isDownloaded: (null) Error: NSPOSIXErrorDomain(2) 
readTokenFileFromUrl: Trying to read Token File. 
getResourceValue: 0 isDownloaded: (null) Error: NSPOSIXErrorDomain(2) 
readTokenFileFromUrl: Token from File: (null) 
readTokenFileFromUrl: Error reading uuid Token from File: NSCocoaErrorDomain(260) 
loadiCloudStore: isUbiquitousItemAtURL: Token File actually does not appear to be present. 
writeTokenFileToUrl: Writing token to file: 6AAA8D81-4BB5-4BF4-AD31-E152D3BADD13

此时,通过developer.icloud.com网站验证该文件是否存在于Ubiquity容器中。然后在名为“Blue iPod touch”的第二台设备上安装并运行该应用程序。

isTokenFilePresentAtUrl: Entered for Device Name: Blue iPod touch
startDownloadingUbiquitousItemAtURL: 0 Error: NSCocoaErrorDomain(4)
isUbiquitousItemAtURL: 0
getResourceValue: 0 isUbiquitousItem: (null) Error: NSPOSIXErrorDomain(2)
getResourceValue: 0 isDownloading: (null) Error: NSPOSIXErrorDomain(2)
getResourceValue: 0 isDownloaded: (null) Error: NSPOSIXErrorDomain(2)
readTokenFileFromUrl: Trying to read Token File.
getResourceValue: 0 isDownloaded: (null) Error: NSPOSIXErrorDomain(2)
readTokenFileFromUrl: Token from File: (null)
readTokenFileFromUrl: Error reading uuid Token from File: NSCocoaErrorDomain(260)

对于此测试,应用程序在尝试读取后停止,并且不允许尝试写入或替换令牌文件。该应用程序随后将被杀死,删除并重新安装并在Blue iPod touch上再次运行。

isTokenFilePresentAtUrl: Entered for Device Name: Blue iPod touch
startDownloadingUbiquitousItemAtURL: 1 Error: (null)(0)
isUbiquitousItemAtURL: 1
getResourceValue: 1 isUbiquitousItem: 1 Error: (null)(0)
getResourceValue: 1 isDownloading: 1 Error: (null)(0)
getResourceValue: 1 isDownloaded: 0 Error: (null)(0)
readTokenFileFromUrl: Trying to read Token File.
getResourceValue: 1 isDownloaded: 0 Error: (null)(0)
readTokenFileFromUrl: Token read from file: 6AAA8D81-4BB5-4BF4-AD31-E152D3BADD13

这次它找到并读取具有预期内容的文件。

1 个答案:

答案 0 :(得分:0)

好吧,我没有放弃,我现在认为我有一个可以接受的解决这个问题的方法。 解决方案的关键是第二个设备能够成功检测第一个设备写入普遍容器的令牌文件,而不是尝试自己编写文件。

以下是适用于我的步骤:

  • 立即尝试从容器中读取令牌文件。
  • 如果成功,请使用该文件并忽略其余步骤。
  • 否则,启动元数据查询,其谓词将触发两个特定文件名(您尝试读取的令牌文件和“虚拟”文件)。
  • 现在使用虚拟文件名将文件写入ubiquity容器。
  • 当元数据查询的结果到达时,请检查其报告的文件的上传/下载状态。
  • 如果查询曾报告看到令牌文件,那么这不是第一个设备,它应该继续尝试读取文件,直到成功。可能需要一段时间,但最终会阅读它。
  • 如果元数据查询报告虚拟文件被完全上传并且仍然没有令牌文件的迹象,则可以安全地假设这是第一个设备,现在可以写入令牌文件了。
  • 我发现在尝试使用令牌之前等待令牌文件的状态完全上传是有用的。否则,如果存在网络问题,另一个也启动此过程的设备可能会做同样的事情,并认为它是第一个设备。

似乎将虚拟文件写入ubiquity容器“踢开”元数据查询足以让它在第二个设备上报告令牌文件的存在。没有它,元数据查询永远不会在第一个设备以外的任何内容上报告令牌文件的存在。

相关问题