gpt4 book ai didi

ios - 如何在iCloud中对UIDocument进行增量写入?

转载 作者:行者123 更新时间:2023-12-01 16:44:15 24 4
gpt4 key购买 nike

我在 iCloud 无处不在的容器中有 UIDocument,我需要在保存文档时将数据附加到文件。我根据 UIDocument 文档覆盖了 readFromURL::writeContents::::: 方法:

-(BOOL) writeContents:(id)contents toURL:(NSURL*)url forSaveOperation:(UIDocumentSaveOperation)saveOperation originalContentsURL:(NSURL*)originalContentsURL error:(NSError *__autoreleasing *)outError{    NSFileCoordinator* coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];    NSError* error = nil;    [coordinator coordinateWritingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {        NSData* data = contents; //correct, non-empty NSData        ///[data writeToFile:newURL :] works, but overwrite original file        NSOutputStream* stream =[[NSOutputStream alloc] initWithURL:newURL append:YES];        if (stream)        {            NSInteger written = [stream write:data.bytes maxLength:data.length];            if (written != data.length)            {                //failed here, written == -1                NSLog(@"Write data to UIDocument failed: %@, error: %@", newURL, stream.streamError);            }        }        else        {            NSLog(@"Write data to iCloudDocument failed: %@", newURL);        }    }];    if (error)    {        NSLog(@"Coordinated write failed %@, error: %@", url, error);        *outError = error;    }    return error == nil;}

访问 block 有不同的newURL,例如:

url: file:///private/var/mobile/Library/Mobile%20Documents/XXXXXXX~com~test~test/test.doc

newURL: file:///private/var/mobile/Applications/5631D484-7661-4E9E-A342-B25297FC0E18/tmp/(A%20Document%20Being%20Saved%20By%20test%20)/test .doc.

[stream write::] 失败,因为 newURL 文件不存在,我无法附加数据,只能创建包含所有文档内容的文件。

文档编辑代码:

NSURL* url = [self.containerURL URLByAppendingPathComponent:kCloudDocumentName];MyDocument* document = [[MyDocument alloc] initWithFileURL:url];[document openWithCompletionHandler:^(BOOL success) {    if (success)    {        //update some document data        [self updateData:document completion:nil];        [document closeWithCompletionHandler:^(BOOL success) {            //failed here!        }];    }}];

MyDocument 存在于位于 url 的无处不在的容器中,并且文档具有 Normal 状态。

在这种情况下我如何进行增量写入?怎么了?

最佳答案

更改 UIDocumnet 中的数据后,您可以使用:

-[UIDocumnet saveToURL:UIDocumnet.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:nil];

不确定,它是否正确,但它在我的应用程序中有效。如果有错,请大家指正。

您可能需要在您的 UIDocumnet 子类中重写此方法:

- (id) contentsForType:(NSString *)typeName error:(NSError **)outError

关于ios - 如何在iCloud中对UIDocument进行增量写入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21426445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com