NSMetadataQuery does not find iCloud files

I used, as described in Apple Docs NSMetadataQuery, to find the iCloud file. I have only one file, and I know its name. My problem is that sometimes this file does not exist (I think because it is not loaded yet) and NSMetadataQuery cannot find it.

Ever tried to force download using NSFileManager startDownloadingUbiquitousItemAtURL:error:, and it returns me an error. (Read EDIT)

My solution is that I created the file for the first time, then I think it exists and I open it with a UIDocument. But it cannot exist or it can be the first time the user opens the application. I can not be sure of this. My first question is: if UIDocument opens the file, it means that it found the file somewhere. How can it use a file if it DOES NOT EXIST?

And then, the second question: If I am an application that needs to manage multiple files or files with an unknown name. How can I find them if NSMetadataQuery does not work.

EDIT: if you startDownloadingUbiquitousItemAtURLshould use to start downloading a file, how do you know when the file has finished downloading (possibly with a notification)? But, more importantly: how to upload a file if it always says (original names removed)?

   Error Domain=NSPOSIXErrorDomain Code=2 "The operation couldn’t be completed.
    No such file or directory" UserInfo=0x166cb0 {
    NSDescription=Unable to get real path for Path
'/private/var/mobile/Library/Mobile Documents/teamid~com~team~app/Documents/file.extension'
    }
+5
3

iCloud. 4 :

  • , iCloud
  • ( , , , *.txt, , , - NSPredicate *pred = [NSPredicate predicateWithFormat:@"NOT %K.pathExtension = '.'", NSMetadataItemFSNameKey]; , jpg, txt, dat ..)
  • , . , . , .

, :

    - (void)loadData:(NSMetadataQuery *)query {

    // (4) iCloud: the heart of the load mechanism: if texts was found, open it and put it into _document; if not create it an then put it into _document

    if ([query resultCount] == 1) {
        // found the file in iCloud
        NSMetadataItem *item = [query resultAtIndex:0];
        NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];

        MyTextDocument *doc = [[MyTextDocument alloc] initWithFileURL:url];
        //_document = doc;
        doc.delegate = self.viewController;
        self.viewController.document = doc;

        [doc openWithCompletionHandler:^(BOOL success) {
            if (success) {
                NSLog(@"AppDelegate: existing document opened from iCloud");
            } else {
                NSLog(@"AppDelegate: existing document failed to open from iCloud");
            }
        }];
    } else {
        // Nothing in iCloud: create a container for file and give it URL
        NSLog(@"AppDelegate: ocument not found in iCloud.");

        NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
        NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"text.txt"];

        MyTextDocument *doc = [[MyTextDocument alloc] initWithFileURL:ubiquitousPackage];
        //_document = doc;
        doc.delegate = self.viewController;
        self.viewController.document = doc;

        [doc saveToURL:[doc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            NSLog(@"AppDelegate: new document save to iCloud");
            [doc openWithCompletionHandler:^(BOOL success) {
                NSLog(@"AppDelegate: new document opened from iCloud");
            }];
        }];
    }
}

- (void)queryDidFinishGathering:(NSNotification *)notification {

    // (3) if Query is finished, this will send the result (i.e. either it found our text.dat or it didn't) to the next function

    NSMetadataQuery *query = [notification object];
    [query disableUpdates];
    [query stopQuery];

    [self loadData:query];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
    _query = nil; // we're done with it
}

-(void)loadDocument {

    // (2) iCloud query: Looks if there exists a file called text.txt in the cloud

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
    _query = query;
    //SCOPE
    [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
    //PREDICATE
    NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K == %@", NSMetadataItemFSNameKey, @"text.txt"];
    [query setPredicate:pred];
    //FINISHED?
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
    [query startQuery];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"AppDelegate: app did finish launching");
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    // (1) iCloud: init

    NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    if (ubiq) {
        NSLog(@"AppDelegate: iCloud access!");
        [self loadDocument];
    } else {
        NSLog(@"AppDelegate: No iCloud access (either you are using simulator or, if you are on your phone, you should check settings");
    }


    return YES;
}
+7

edo42, ? 4S, AT & T, 4S, 4S, .

  • [iCloudFileURL getResource: & fileState forKey: NSURLIsUbiquitousItemKey: & ] .

= NSPOSIXErrorDomain Code = 2 " " UserInfo = 0x448500 {NSDescription = Path '/private/var/mobile/Library/Mobile Documents/~ UbiquitousDir ~//MyDocument.ext

( iPad 2)

  • NSMetadataQuery, - . ( iPad 2 , iCloud).

  • , , , , .

  • [fileMgr evictUbiquitousItemAtURL: ubiquitousDocumentsDir & ], . NSMetadataQuery, - .

, , .

UPDATE: , iCool , :

  • iPhone.
  • " ".
  • iCloud ( ) .
  • iCloud , iPad 2, .
  • iPad 2, iCloud.
  • iCloud iPhone.
  • , NSMetadaQuery.
+1

, metadataQueryDidUpdate metadataQueryDidFinishGathering , NSMetadataQuery . , Xcode , , , , ( ).

What is fixed for me is in the settings and click on the "Download All" button for the preparation profiles, and then perform "Clear in Xcode". Rebuild, run, and all my files appeared. Check the developer portal to make sure that none of your profiles are marked as "False." None of mine were, so he shouldn't have started it, but it could happen.

Not quite scientific, but it worked for me.

0
source

All Articles