Can i use NSMetadataQuery when iCloud is disabled?

I cannot get NSMetadataQuery to work when i turn off iCloud. I set the correct search url but it never registers as completed:

//Check for iCloud
NSURL *ubiq = [[NSFileManager defaultManager] 
               URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
    NSLog(@"iCloud access at %@", ubiq);
    self.query = [[[NSMetadataQuery alloc] init] autorelease];
    [self.query setSearchScopes:[NSArray arrayWithObject:
                                 NSMetadataQueryUbiquitousDataScope]];
    _isiCloudEnabled = YES;
} else {
    NSLog(@"No iCloud access");
    //Get the doc directory
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    self.query = [[[NSMetadataQuery alloc] init] autorelease];
    [self.query setSearchScopes:[NSArray arrayWithObjects:
                                 [NSURL fileURLWithPath:path],nil]];
    _isiCloudEnabled = NO;
}

NSPredicate *pred = [NSPredicate predicateWithFormat: 
                     @"%K like %@", NSMetadataItemFSNameKey, @"*.adoc"];
[self.query setPredicate:pred];
[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(queryDidFinishGathering:) 
 name:NSMetadataQueryDidFinishGatheringNotification 
 object:self.query];

[self.query startQuery];

queryDidFinishGathering: never called. When iCloud is on, it is always called. Any idea why?

+3
source share
2 answers

I had the same problem, but I use ARC in the project. This can be solved by setting the ivar variable in self.query.

@property (nonatomic, strong) NSMetadataQuery *query;

I think for your problem, since you are not using ARC, you may need to do the following:

  • delete the autorun and manually release when finished.
  • you need to set property

    @property (nonatomic, retain) NSMetadataQuery *query;
    
+2
source

iOS5, NSMetadataQuery ​​ (NSMetadataQueryUbiquitousDocumentsScope NSMetadataQueryUbiquitousDataScope), iCloud .

, , , queryDidFinishGathering - , ( :))

, NSMetadataQuery iOS, OSX , NSPredicate, NSSortDescriptors ..

0

All Articles