grand-central-dispatch

Why should I choose GCD over NSOperation and blocks for high-level applications?

本小妞迷上赌 提交于 2019-11-28 03:12:25
Apple's Grand Central Dispatch reference says: "...if your application needs to operate at the Unix level of the system—for example, if it needs to manipulate file descriptors, Mach ports, signals, or timers. GCD is not restricted to system-level applications, but before you use it for higher-level applications, you should consider whether similar functionality provided in Cocoa (via NSOperation and block objects) would be easier to use or more appropriate for your needs.". http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

Grand Central Dispatch (GCD) with CoreData

北慕城南 提交于 2019-11-28 02:44:13
I'm using Grand Central Dispatch (GCD) in my application to do some heavy lifting. The application is using Core-Data for data storage purposes. Here's my scenario (along with relevant question): dispatch_queue_t main_queue = dispatch_get_main_queue(); dispatch_queue_t request_queue = dispatch_queue_create("com.app.request", NULL); dispatch_async(request_queue, ^{ MyNSManagedObject *mObject = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; // … // <heavy lifting> // … // … // <update mObject> // … [self saveManagedObjectContext]; }); As a result

“Pausing” the Game in Swift

梦想的初衷 提交于 2019-11-28 02:26:17
问题 I created a game in Swift that involves monsters appearing. Monsters appear, and disappear, based on timers using something like this: func RunAfterDelay(_ delay: TimeInterval, block: @escaping ()->()) { let time = DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) DispatchQueue.main.asyncAfter(deadline: time, execute: block) } and then I'd just call it like this (for example to spawn after 2 seconds): ///Spawn Monster RunAfterDelay(2) { [unowned self] in

Unhiding a view is very slow in CTCallCenter callEventHandler

落爺英雄遲暮 提交于 2019-11-27 21:23:18
问题 Reposting with more concise and focused question after original question went unanswered. Also adding more insight into the problem after another day of research: In my app delegate ( didFinishLaunching ), I set up a callEventHandler on CTCallCenter . The idea is that when a callState changes, I post a notification with a userInfo dict containing the call.callState . In my view, I observe this notification, and when the userInfo dict contains a value of CTCallDisconnected , I want to unhide a

dispatch_sync always scheduling a block on Main Thread

半世苍凉 提交于 2019-11-27 20:47:23
问题 I am executing a block using dispatch_sync and the block is executed correctly. But this block is executed on the main thread. As per the Apple Doc: Serial queues (also known as private dispatch queues) execute one task at a time in the order in which they are added to the queue. The currently executing task runs on a distinct thread (which can vary from task to task) that is managed by the dispatch queue. which means (or what I understood) that current process that is being executed will run

Waiting for multiple asynchronous download tasks

一笑奈何 提交于 2019-11-27 19:16:48
问题 I want to download some files, for example 100 files, at the same time. So I decided to add my download threads to a dispatch queue, and GCD will adjust how many threads will run at the same time. The problem here is: the block in dispatch_async will be completed immediately, because task will run on another thread. So, if urls 's length is 100, it will create 100 threads immediately. var queueDownloadTask = dispatch_queue_create("downloadQueue", nil) for url in urls { dispatch_async

What property should I use for a Dispatch Queue after ARC?

南笙酒味 提交于 2019-11-27 19:09:20
问题 I maintain a dispatch queue as a property with my view controller. I create this queue once in my view controller's init method, and reuse a few times for some background tasks. Before ARC, I was doing this: @property (nonatomic, assign) dispatch_queue_t filterMainQueue; And in init: if (filterMainQueue == nil) { filterMainQueue = dispatch_queue_create("com.myQueue.CJFilterMainQueue", NULL); } But after ARC, I'm not sure if this should still be "assign", or should it be "strong" or "weak".

DispatchSourceTimer and Swift 3.0

夙愿已清 提交于 2019-11-27 19:06:00
I can't figure out how to make dispatch timer work repeatedly in Swift 3.0. My code: let queue = DispatchQueue(label: "com.firm.app.timer", attributes: DispatchQueue.Attributes.concurrent) let timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: UInt(0)), queue: queue) timer.scheduleRepeating(deadline: DispatchTime.now(), interval: .seconds(5), leeway: .seconds(1) ) timer.setEventHandler(handler: { //a bunch of code here }) timer.resume() Timer just fires one time and doesn't repeat itself like it should be. How can I fix this? Make sure the timer doesn't fall out

Is it necessary to create an autorelease pool under ARC in GCD?

ⅰ亾dé卋堺 提交于 2019-11-27 18:55:33
问题 I have a run loop method for a CAEAGLLayer which uses GCD for serializing access to shared ivars. My drawing code currently is constructed like this: - (void)draw { dispatch_sync(serialDrawingQueue, ^{ @autoreleasepool { [self drawingStart]; [spriteA draw]; [spriteB draw]; [self drawingEnd]; } }); } The draw method is called by a CADisplayLink. Is the @autoreleasepool necessary when I use GCD blocks? 回答1: From the Apple docs: If your block creates more than a few Objective-C objects, you

Run repeating NSTimer with GCD?

别说谁变了你拦得住时间么 提交于 2019-11-27 18:35:11
I was wondering why when you create a repeating timer in a GCD block it doesen't work? This works fine: -(void)viewDidLoad{ [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; } -(void)runTimer{ NSLog(@"hi"); } But this doesent work: dispatch_queue_t myQueue; -(void)viewDidLoad{ [super viewDidLoad]; myQueue = dispatch_queue_create("someDescription", NULL); dispatch_async(myQueue, ^{ [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; }); } -(void)runTimer