swift - NSMetadataQuery doesn't return any data -


i try list of file on mac app made. use nsmetadataquery it's not working.

here code:

import cocoa  class viewcontroller: nsviewcontroller {  let metadataquery = nsmetadataquery()  @iboutlet weak var searchtextfield: nstextfield!  @iboutlet weak var labelml: nstextfield!  @ibaction func searchclick(sender: anyobject) {     labelml.stringvalue = "hello \(searchtextfield.stringvalue)!"     startquery()     handlemetadataqueryfinished(metadataquery) }  override func viewdidload() {     super.viewdidload()      // additional setup after loading view. }   func startquery() {     print("starting query now...")      metadataquery.searchscopes = [nsmetadataqueryubiquitousdatascope]     let predicate = nspredicate(format: "%k ==[cd] '*'", nsmetadataitemfsnamekey)      metadataquery.predicate = predicate     if metadataquery.startquery(){         print("successfully started query.")     } else {         print("failed start query.")     }  }  func handlemetadataqueryfinished(sender: nsmetadataquery) {      print("search finished");     metadataquery.disableupdates()     metadataquery.stopquery()     print("number of results \(metadataquery.resultcount)")      item in metadataquery.results as! [nsmetadataitem]     {          let itemname = item.valueforattribute(nsmetadataitemfsnamekey)             as! string          let itemurl = item.valueforattribute(nsmetadataitemurlkey)             as! nsurl          let itemsize = item.valueforattribute(nsmetadataitemfssizekey)             as! int         print("item name = \(itemname)")         print("item url = \(itemurl)")         print("item size = \(itemsize)")      }      }  } 

as can see, print number of results of metaquery , answers 0.

i've try change things nsmetadataqueryindexedlocalcomputerscope instead of nsmetadataqueryubiquitousdatascope or format of predicate either way it's not working.

any idea why?

you should register observer nsmetadataquerydidfinishgatheringnotification , wait till called. search takes little while. , did starting query return true?

here objective-c style example code:

#import "cloudutils.h"  @interface cloudutils () @property(nonatomic, strong) nsmetadataquery *query; @end  @implementation cloudutils  static cloudutils *singleton;  + (cloudutils *) sharedinstance {     if (singleton == nil) {         singleton = [[cloudutils alloc] init];     }     return singleton; }  + (void) updateclouddrive {     nslog(@"in updateclouddrive");      cloudutils *utils = [cloudutils sharedinstance];      // wichtig: das query muss strong gebunden sein... sonst ist das zu früh wieder weg!     utils.query              = [[nsmetadataquery alloc] init];     utils.query.searchscopes = [nsarray arraywithobjects:nsmetadataqueryubiquitousdocumentsscope, nsmetadataqueryubiquitousdatascope,nil];     utils.query.predicate    = [nspredicate predicatewithformat:@"%k like[cd] %@", nsmetadataitemfsnamekey, @"*"];      [[nsnotificationcenter defaultcenter] addobserver:utils                                              selector:@selector(querydidfinishgathering:)                                                  name:nsmetadataquerydidfinishgatheringnotification                                                object:utils.query];      [[nsnotificationcenter defaultcenter] addobserver:utils                                              selector:@selector(querydidupdate:)                                                  name:nsmetadataquerydidupdatenotification                                                object:utils.query];      dispatch_async(dispatch_get_main_queue(), ^{         // das scheitert, falls schon ein solches query läuft... aber nicht schlimm ist.         [utils.query startquery];     }); }  // diese methode kommt ins spiel, wenn es zu viele ergebnisse auf einmal sind... // dann werden einige davon schon gemeldet, bevor das query ganz fertig ist... - (void) querydidupdate: (nsnotification *) notification {     nslog(@"in querydidupdate:");      nsmetadataquery *query = [notification object];     [query disableupdates];     nserror *error = nil;     (nsmetadataitem *item in [query results]) {         nsurl *url = [item valueforattribute:nsmetadataitemurlkey];         nslog(@"starting download of %@", url);         [[nsfilemanager defaultmanager] startdownloadingubiquitousitematurl:url error:&error];     }     [query enableupdates]; }  - (void) querydidfinishgathering: (nsnotification *) notification {     nslog(@"in querydidfinishgathering:");      nsmetadataquery *query = [notification object];     [query disableupdates];     [query stopquery];     [[nsnotificationcenter defaultcenter] removeobserver:self name:nsmetadataquerydidfinishgatheringnotification object:query];     [[nsnotificationcenter defaultcenter] removeobserver:self name:nsmetadataquerydidupdatenotification          object:query];      nserror *error = nil;     (nsmetadataitem *item in [query results]) {         nsurl *url = [item valueforattribute:nsmetadataitemurlkey];         nslog(@"starting download of %@", url);         [[nsfilemanager defaultmanager] startdownloadingubiquitousitematurl:url error:&error];     } }  @end 

Comments