0

我目前在核心数据方面遇到了一个奇怪的问题。我正在编写的应用程序从服务器下载一堆数据,这些数据被转换为核心数据对象并存储。该设备还可以创建新对象并将它们上传到服务器。其中一个对象是 Document,它本质上是文件的表示。

这个模型是MPDocument。一个文档也可以链接到一个MPPlace模型,一个MPUser模型(用户创建文档,文档属于地方)。

我从服务器下载对象没有问题,并且所有关系都被正确创建和分配。当我尝试在设备本身上创建一个新文档时,问题就出现了。文档被创建,我设置了所有的关系,文档被上传,一切看起来都很好。但是当我通过核心数据查看器工具检查数据库时,所有的文档对象都没有位置关系的价值。这发生在所有现有文件上,而不仅仅是新文件。我实在想不通是怎么回事!

我正在创建这样的文档:

   MPUser *current = [MPUser currentUser];
   MPDocument *doc = [[MPDocument alloc] init];
          doc.name = @"App Upload";
     doc.local_url = [NSString stringWithFormat:@"%@", [info valueForKey:UIImagePickerControllerReferenceURL]];
doc.local_url_type = @(MPDocumentUrlTypeAsset);
          doc.user = current;

[current addCreatedDocumentsObject:doc];
[doc setValue:self.place forKey:@"place"];
[self.place addDocumentsObject:doc];

然后我有一个文件上传器来处理所有上传:

MPDocumentUploader *uploader = [[MPDocumentUploader alloc] initWithDocument:doc];
    uploader.requestDelegate = self;
       uploader.successBlock = ^(MPDocumentUploader *uploader, MPDocument *doc) {
           NSLog(@"Got doc = %@", doc);
       };
[uploader upload];

当调用成功块时,文档对象确实设置了位置关系。因此,即使上传完成,位置也已设置,所以我现在真的很困惑关系在哪里被完全清除。

文档上传器看起来像这样:

- (void) upload
{
    .... retrieve the local file and turn into NSData. This is fine
    MPRequest *request = [MPRequest requestWithURL:_url];
    [MPUser signRequest:request];
    [request setDelegate:_requestDelegate];
    [request setRequestMethod:@"POST"];
    [request mountDocumentUploader:self];

    [request submit:^(MPResponse *resp, NSError *error) {
        if (!error) {
            NSDictionary *data = (NSDictionary *)[resp paramForKey:@"data"];
            if (data) {
                NSLog(@"Document = %@", _document);
                _document.url = [data objectForKey:@"url"];
                _document.objID = [data objectForKey:@"id"];
                [_document saveLocally];
            }

            if (_successBlock) {
                _successBlock(self, _document);
            }
        } else {
            if (_failBlock) {
                _failBlock(self, error);
            }
        }
    }];
}

该类MPRequest处理所有实际的上传和服务器请求,但实际上并不涉及 MPDocument 对象。

我不知道发生了什么,也不知道为什么要清除这些关系。请问有人可以帮忙吗!?

更新

玩了一圈,发现是调用提交块的时候报错。注释掉

_document.url = [data objectForKey:@"url"];
_document.objID = [data objectForKey:@"id"];
[_document saveLocally];

按预期工作,但现在显然没有设置这些值。将这些行中的任何一条单独添加回 1 仍然会导致问题,因此似乎只是简单地编辑它就会破坏它。仍然不知道为什么:(

4

1 回答 1

0

好吧,我不完全确定发生了什么或为什么,但是在摆弄代码、清理项目和重新启动我的笔记本电脑之间,它似乎已经修复了自己,所以不知道问题是什么,但目前似乎没问题. 很困惑:S

于 2013-01-03T04:13:32.927 回答