如何从iCloud中删除文件而不先将其下载到设备中?

时间:2015-02-12 09:07:22

标签: ios cocoa-touch icloud nsfilemanager

我有一个显示文档列表的基本iOS应用程序。我正在尝试删除文档,但注意到如果文档尚未从iCloud下载到设备,则下面的代码将失败并显示“没有此类文件或目录”

文档可能非常大(40MB),我想避免下载文档只是为了删除它(这需要时间和带宽超出用户的数据计划)。这有可能吗?

[[[NSFileCoordinator alloc] initWithFilePresenter:nil]
coordinateWritingItemAtURL:documentURL
                  options:NSFileCoordinatorWritingForDeleting
         writingItemAtURL:previewURL
                  options:NSFileCoordinatorWritingForDeleting
                    error:&error
               byAccessor:^(NSURL *newDocumentURL, NSURL *newPreviewURL){

    // Fails with "No such file" error if not yet downloaded from iCloud:
    [[NSFileManager defaultManager] removeItemAtURL:newDocumentURL error:&error];
    [[NSFileManager defaultManager] removeItemAtURL:newPreviewURL  error:&error];
}];

完整错误:

Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. 
(Cocoa error 4.)" UserInfo=0x14e82930 {NSUnderlyingError=0x14e69220 
"The operation couldn’t be completed. No such file or directory",

2 个答案:

答案 0 :(得分:3)

注意:Apple曾经有一些示例代码来说明这一点,但它已被删除。

正如在答案中指出的那样,您需要在循环中调用此方法,因为您需要为要删除的每个文件单独NSFileCoordinator

我错过的一点是,您必须使用您为删除而创建的fileManager.removeItemAtURL对象的网址调用NSFileAccessIntent正常URL您可以从NSMetadataQueryItem获得访问权限。

func removeFile(at url: URL, completionHandler: ((Error?) -> Void)? = nil) {
    // `url` may be a security scoped resource.
    let successfulSecurityScopedResourceAccess = url.startAccessingSecurityScopedResource()

    let fileCoordinator = NSFileCoordinator()
    let writingIntent = NSFileAccessIntent.writingIntent(with: url, options: .forDeleting)
    fileCoordinator.coordinate(with: [writingIntent], queue: backgroundQueue) { (accessError) in
        if accessError != nil {
            completionHandler?(accessError)
            return
        }

        let fileManager = FileManager()
        var error: Error?
        do {
            try fileManager.removeItem(at: writingIntent.url)
        }
        catch let fileError {
            error = fileError
        }
        if successfulSecurityScopedResourceAccess {
            url.stopAccessingSecurityScopedResource()
        }

        completionHandler?(error)
    }
}

如果您想删除多个项目:

for url in urlsToDelete {
    removeFile(at: url)
}

答案 1 :(得分:1)

如果您只需要删除文件,请使用另一个NSFileCoordinator coordinateWritingItemAtURL(访问者块中具有单个newURL参数的那个)。

如果需要批量删除,则创建一个NSFileAccessIntent数组并使用NSFileCoordinator的coordinateAccessWithIntents。

示例:

- ( void )deleteItemsAtURLs: ( NSArray * )urls queue: ( NSOperationQueue * )queue
{
    //assuming urls is an array of urls to be deleted
    NSFileCoordinator   * coordinator;
    NSMutableArray      * writingIntents;
    NSURL               * url;

    writingIntents = [ NSMutableArray arrayWithCapacity: urls.count ];

    for( url in urls )
    {
        [ writingIntents addObject: [ NSFileAccessIntent writingIntentWithURL: url options: NSFileCoordinatorWritingForDeleting ] ];
    }

    coordinator = [ [ NSFileCoordinator alloc ] initWithFilePresenter: nil ];

    [ coordinator coordinateAccessWithIntents: writingIntents
                                        queue: queue
                                   byAccessor: ^( NSError * error )
     {
         if( error )
         {
             //handle
             return;
         }
         NSFileAccessIntent * intent;

         error = nil;

         for( intent in writingIntents )
         {
             [ [ NSFileManager defaultManager ] removeItemAtURL: intent.URL error: &error ];
             if( error )
             {
                 //handle
             }
         }
     }];
}