ios - iCloud connects on simulator but not on device -
i'm new in programming ios , have got problem couldn't icloud connection if run app on real device.
app runs perfect on simulator , if disconnect device mac.
have idea how can fix it?
code have written connect icloud
self.filemanager = [nsfilemanager defaultmanager]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ self.ubiquityidentifier = [self.filemanager urlforubiquitycontaineridentifier:nil]; }); dispatch_async(dispatch_get_main_queue(), ^{ if(self.ubiquityidentifier) { [[nsnotificationcenter defaultcenter] postnotificationname:kicloudavailablenotification object:self.ubiquityidentifier]; [self startmediaquery]; } else { [[nsnotificationcenter defaultcenter] postnotificationname:kicloudnotavailablenotification object:self.ubiquityidentifier]; nslog(@"icloud not available"); } });
if device connected go in else block , if test in simulator or on not connected device works fine.
lot.
weissja19
assuming you're detecting failure because nslog
prints "icloud not available" message, you're setting classic race condition here:
- you set value of
self.ubiquityidentifier
on global (non-main) queue. - you test value
self.ubiquityidentifier
on main queue.
the 2 dispatch_async
calls run on different queues. result, there's no guarantee you're setting value of self.ubiquityidentifier
before test it. work sometimes, , fail @ others, , not obvious reason. when "icloud not available" message, it's because second queue checked value before assigned value.
the fix change dispatch calls can guarantee order of execution. 2 possibilities are:
- combine these 2
dispatch_async
calls of code happens on same queue, or - move second
dispatch_async
inside first, don't dispatch main queue until you've set valueself.ubiquityidentifier
.
Comments
Post a Comment