grand-central-dispatch

Load a big plist from url in background

我的未来我决定 提交于 2019-12-24 03:02:08
问题 I load a big plist file from url and I must wait for some seconds before I can use the application. Is there some solution? How it can be loaded in background? Is GCD what I need? How it can be implemented? My code: NSString *urlStr = [[NSString alloc] initWithFormat:@"http://www.domain.com/data.xml"]; NSURL *url = [NSURL URLWithString:urlStr]; NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url]; 回答1: You could create a new thread to do so Please check the following //Create

Is the creation of an UIKit object in background using GCD a bad practice?

拈花ヽ惹草 提交于 2019-12-24 02:23:34
问题 As pointed out by bbum here, the doc says: " For the most part, UIKit classes should only be used from the main thread of an application This is especially true for derived classes UIResponder or involve the manipulation of user interface of your application in any way. ". I thought I understood that the methods of drawings could not be called in a background thread, so that the creation could be done in the background, as the drawRect method is only called when the view is added. But maybe i

Cannot refer to declaration with a variably modified type inside block [duplicate]

被刻印的时光 ゝ 提交于 2019-12-24 01:44:44
问题 This question already has answers here : How to write into an array from a dispatch_apply (GCD) loop? (2 answers) Closed 5 years ago . This is the snippet of my code and i am getting the above mentioned error. dispatch_async(background_thread, ^{ cameras camera[10]; . . for(int i=0; i<ncam; i++) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{ iPoint subPoints[10]; subPoints[0].x = camera[i].x + ((int)camera[i].mindist)*cos(camera[i].dir + (camera[i].angle)/2);

NSTask Race Condition With ReadabilityHandler Block

ぐ巨炮叔叔 提交于 2019-12-24 01:13:25
问题 Basic Setup I use NSTask to run a process that optimizes images. This process writes output data to stdout . I use the readabilityHandler property of NSTask to capture that data. Here is the abbreviated setup: NSTask *task = [[NSTask alloc] init]; [task setArguments:arguments]; // arguments defined above NSPipe *errorPipe = [NSPipe pipe]; [task setStandardError:errorPipe]; NSFileHandle *errorFileHandle = [errorPipe fileHandleForReading]; NSPipe *outputPipe = [NSPipe pipe]; [task

How to wait past dispatch_async before proceeding?

♀尐吖头ヾ 提交于 2019-12-23 20:30:57
问题 I have a series of dispatch_async that I am performing and I would like to only update the UI when they are all done. Problem is the method within dispatch_async calls something in a separate thread so it returns before the data is fully loaded and dispatch_group_notify is called before everything is loaded. So I introduce a infinite loop to make it wait until a flag is set. Is this the best way? See code below. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,

How to deal with concurrency issues brought by NSStream run loop scheduling using GCD?

核能气质少年 提交于 2019-12-23 18:19:36
问题 I have the following situation where I create a GCD dispatch queue and in it I schedule an NSStream to the current NSRunLoop , as is required in its specification for it to emit delegate events, and then I make the run loop for that thread run using [[NSRunLoop currentRunLoop run] . This generates three possible scenarios: Create a serial queue in which an initial write message is sent through the stream and other write messages are only sent when there's a delegate callback from the NSStream

NSManagedObjectContext's performBlockAndWait: not executing on the receiver's queue

核能气质少年 提交于 2019-12-23 13:34:05
问题 I've noticed that it's possible for an NSManagedObjectContext with an NSMainQueueConcurrencyType to performBlockAndWait : and execute the block on a queue other than the receiver's (main) queue. For example, the following code results in my parentContext executing the block on the childContext 's queue if my parentContext is of type NSMainQueueConcurrencyType and my childContext is of type NSPrivateQueueConcurrencyType : [childContext performBlockAndWait:^{ //Thread 1, Queue:

Which thread does the block in dispatch_once run in?

萝らか妹 提交于 2019-12-23 13:15:38
问题 Which thread does the block in dispatch_once run in? Can dispatch_once block run in background thread if the code is run from main thread? How do I ensure it runs on the main thread regardless of which thread executes it? 回答1: It runs in the current/calling thread. If you wanted I suppose you could use dispatch_sync to ensure it runs on a background thread but I'm not sure what that'd get you. In sum, it runs in the current thread. If another thread is already in the dispatch_once block, the

DispatchGroup: check how many “entered”

倖福魔咒の 提交于 2019-12-23 10:17:41
问题 I'm using Swift 3 DispatchGroup to wait until multiple async operations are finished (according to this answer which works perfect and as expected. Is there a way to check how many operations are entered already, like dispatchGroup.count or something like that? 回答1: NO, nothing. You need to balance your enter() and leave() by yourself without counting. DispatchGroup 回答2: You can see counts of enters to the group in debug description: OS_dispatch_group: group[0x60000221d950] = { xref = 3, ref

ALAssetLibrary Notification and enumerateAssetsUsingBlock

若如初见. 提交于 2019-12-23 05:37:22
问题 I'm building an iOS app that allows a user to take a picture and save it to a custom album, which populates a UICollectionView in the app with the photos from the album. I found plenty of examples on the web on how to do this, but I could NOT get past a stubborn problem with the most recent picture not showing up in the album. I ended building a work around using notifications and a serial dispatch queue, but I think it can be improved. Here's how I'm saving the pictures and then repopulating