0

I have created a UIDocument to wrap a custom object. The UIDocument is successfully saving to iCloud, and I am successfully loading each UIDocument when I run the app each time. However, when I attempt to run the app on a new device (iPhone 5) it will not load any UIDocument from iCloud, but I can check iCloud under Settings and see the files that are saved under my app. Does anyone have some steps to check to see why one device (iPhone 4) can load and the other (iPhone 5) cannot? I also cannot create a new UIDocument from the iPhone 5 device.

EDIT

- (void)loadObjects
{
    if (!self.objects)
    {
        self.objects = [[NSMutableArray alloc] init];
    }

    NSURL *baseURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

    if (baseURL)
    {
        self.query = [[NSMetadataQuery alloc] init];
        [self.query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like 'Object_*'", NSMetadataItemFSNameKey];
        [self.query setPredicate:predicate];

        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self selector:@selector(queryDidFinish:) name:NSMetadataQueryDidFinishGatheringNotification object:self.query];
        [nc addObserver:self selector:@selector(queryDidUpdate:) name:NSMetadataQueryDidUpdateNotification object:self.query];

        [self.query startQuery];
    }
}

- (void)queryDidFinish:(NSNotification *)notification
{
    [self processQueryResults:notification.object];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:notification.object];

    self.query = nil;
}

- (void)queryDidUpdate:(NSNotification *)notification
{
    [self processQueryResults:notification.object];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:notification.object];

    self.query = nil;
}

- (void)processQueryResults:(NSMetadataQuery *)query
{
    [query disableUpdates];

    [query stopQuery];

    [self.objects removeAllObjects];

    [query.results enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
    {
        NSURL *documentURL = [(NSMetadataItem *)obj valueForAttribute:NSMetadataItemURLKey];

        ObjectDocument *document = [[ObjectDocument alloc] initWithFileURL:documentURL];

        [document openWithCompletionHandler:^(BOOL success) {
            if (success) {
                [self.objects addObject:document];

                [self sortObjects];

                [self.tableView reloadData];
            }
        }];
    }];

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)save:(Object *)object
{
    NSURL *baseURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

    if (baseURL)
    {
        NSURL *documentsURL = [baseURL URLByAppendingPathComponent:@"Documents"];

        NSURL *documentURL = [documentsURL URLByAppendingPathComponent:[NSString stringWithFormat:@"Object_%f", [object.date timeIntervalSince1970]]];

        TVBFlightDocument *document = [[ObjectDocument alloc] initWithFileURL:documentURL];
        document.object = object;

        [document saveToURL:documentURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                NSLog(@"Save succeeded.");

                [_objects addObject:document];

                [self sortObjects];
            } else {
                NSLog(@"Save failed.");
            }
        }];
    }
}

The app on the iPhone 5 will not load the objects stored in iCloud, but iPhone 4 will load the objects from the iCloud documents.

bdparrish
  • 3,216
  • 3
  • 37
  • 58

0 Answers0