ios - Is it ok to use UIPasteboard in a background thread? -
is uipasteboard thread safe ?
i trying this
dispatch_async(dispatch_get_global_queue(0, 0), ^{ uipasteboard *generalpasteboard = [uipasteboard generalpasteboard]; nsdata *settingsdata = [generalpasteboard dataforpasteboardtype:@"sometype"]; if (settingsdata == nil) { uipasteboard *pasteboard = [uipasteboard pasteboardwithname:@"somename" create:yes]; settingsdata = [pasteboard dataforpasteboardtype:@"sometype"]; } // settingsdata });
is safe or should use uipasteboard on main thread ?
i'm using on background thread on ios 9 , 10 no issues. pasteboard accesses global, shared system resources, assume thread safe, though in uikit framework. there no documentation me up, own experience.
example code, using category created mbprogresshud:
typedef void (^imageblock)(uiimage* image); #define dispatch_async_global(code) dispatch_async(dispatch_get_global_queue(0, 0), ^{ code }); #define dispatch_async_main(code) dispatch_async(dispatch_get_main_queue(), ^{ code }); + (void) pasteboardimagewithcompletion:(imageblock)block { // show hud in main window mbprogresshud* hud = [mbprogresshud showhudanimated:yes]; dispatch_async_global ({ uiimage* img = [uipasteboard generalpasteboard].image; if (img == nil) { nsurl* url = [uipasteboard generalpasteboard].url; if (url == nil) { nsdata* data = [[uipasteboard generalpasteboard] dataforpasteboardtype:(nsstring*)kuttypeimage]; img = [uiimage imagewithdata:data]; } else { img = [uiimage imagewithdata:[nsdata datawithcontentsofurl:url]]; } } dispatch_async_main ({ block(img); [hud hideanimated:yes]; }); }); }
Comments
Post a Comment