grand-central-dispatch

Why is it necessary to call dispatch_group_leave the same number of times as dispatch_group_enter?

牧云@^-^@ 提交于 2019-12-22 04:40:36
问题 In my dispatch_group code, I use dispatch_group_wait to time out a group of web service calls. Question, it seems like I would need to count how many times dispatch_group_enter is called and then call the same number of remaining dispatch_group_leave should some web service calls never return causing an unequal number of dispatch_group_enter vs dispatch_group_leave. Why? I have seen crashes happening if I don't do this in the case when timeout happens and triggers dispatch_group_wait. The

How can i download photos by order of cells in UITableView with dispatch_async?

不羁的心 提交于 2019-12-22 01:26:15
问题 I have a UITableView that has photos, i get these photos from URLs and i use the block for downloading these photo asynchronously, and i want these photos be downloaded by order of the cells in the UITableView? // Download the images from the review of the businesses and put them into the "imageArray" dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://feature

Grand Central Dispatch and unit testing

北慕城南 提交于 2019-12-22 00:29:39
问题 I have written a simple test case that follows Apple's documentation and I am not seeing the results that I'm expecting. Here's the code: - (void)testExample2 { NSLog(@"1"); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"3"); dispatch_semaphore_signal(semaphore); }); NSLog(@"2"); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); NSLog(@"4"); dispatch_release(semaphore); } I would expect to read: 1, 2, 3, 4 but

SQLite Error : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) -iOS

强颜欢笑 提交于 2019-12-22 00:19:07
问题 When I am trying to insert data into my table, sometimes I get this error, and my app crashes! Crash logs : Observation(4001,0x10dd67000) malloc: *** error for object 0x7fff3a917100: Non-aligned pointer being freed (2) *** set a breakpoint in malloc_error_break to debug 2016-11-03 11:12:03.063 Observation[4001:46477] Insertion failed ! Printing description of dbpath: (const char *) dbpath = 0x00007fff3b8a5690 "/Users/macbt/Library/Developer/CoreSimulator/Devices/0EEC62AE-6DF0-4FC4-9D30

using the same dispatch queue in a method for background processing

僤鯓⒐⒋嵵緔 提交于 2019-12-21 21:34:59
问题 I have a method that updates two sections in a table that takes awhile. I want to do something like: dispatch_queue_t lowQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); dispatch_queue_t mainQueue = dispatch_get_main_queue(); dispatch_async(lowQueue, ^{ NSArray *tempArray = // do long running task to get the data dispatch_async(mainQueue, ^{ // update the main thread [self.activityIndicatorView stopAnimating]; [self.reportsTableView reloadData]; }); }); dispatch_async

Why does my threading seem to fail after a few threads have started on iOS?

谁都会走 提交于 2019-12-21 20:36:38
问题 I have this code in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath delegate call: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[webUrls objectAtIndex:indexPath.row]]; CMTime timeduration = playerItem.duration; float seconds = CMTimeGetSeconds(timeduration); NSString *duration = [NSString stringWithFormat:@"%f", seconds]; dispatch_async(

syscall_thread_switch iOS 8.3 race - CocoaLumberjack bug? how to debug this?

百般思念 提交于 2019-12-21 20:08:40
问题 I'm hitting a race-condition in my app, where all or all but 1 threads get stuck on syscall_thread_switch whenever I pause debugging. It reproduces much more often on the simulator, but also on the iPad Air. There is ALWAYS at least 2 threads stuck in CocoaLumberjack's queueLogMessage: -- see screenshots. I've never seen this before on 8.1 and 8.2, but i'm hitting it often on 8.3. I'm not claiming this is an 8.3 bug :) This is a level of complexity i've never had to debug before, so i'm not

GCD pattern for chaining async operations while piping the results

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 17:18:51
问题 Coming from JavaScript world using async javascript promises, and I believe the same is done using GCD async queues in Swift. Can you point me to an example where 2 to 3 async functions are specified in a queue, with one async operation feeding the result to the second, and second feeding the result to the third (commonly known as piping the results) and then finally a result and error handler. All functions restrict to a single argument by design. If any error let's say during function 2,

How to lock an NSLock on a specific thread

无人久伴 提交于 2019-12-21 12:27:23
问题 I have a property @property NSLock *myLock And I want to write two methods: - (void) lock and - (void) unlock These methods lock and unlock myLock respectively and they need to do this regardless of what thread or queue called them. For instance, thread A might have called lock but queue B might be the one calling unlock . Both of these methods should work appropriately without reporting that I am trying to unlock a lock from a different thread/queue that locked it. Additionally, they need to

When to use NSEnumerationConcurrent

人走茶凉 提交于 2019-12-21 07:55:15
问题 Every now and then, I notice that I'm using a block to iterate over a collection without writing to any shared data or causing any side effects. I consider adding in an NSEnumerationConcurrent option, then decide against it as I don't really understand when it's worth using. So I've got a specific question, and a more general one. First question: Here's a maybe slightly contrived example of using a block to do something trivial concurrently: CGFloat GetAverageHeight(NSArray* people) {