grand-central-dispatch

In unit test, execute the block passed in queue with dispatch_asyc

戏子无情 提交于 2019-12-21 03:01:21
问题 If I dispatch_async a block on main queue like this: -(void) myTask { dispatch_async(dispatch_get_main_queue(), ^{ [self.service fetchData]; }); } In unit test, I can execute the block passed in main queue by manually run the main loop like this: -(void)testMyTask{ // call function under test [myObj myTask]; // run the main loop manually! [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; // now I can verify the function 'fetchData' in block is called ... } Now

HTTP Long Polling in Swift

旧城冷巷雨未停 提交于 2019-12-21 02:41:32
问题 I am trying to implement a long-polling solution in Swift using iOS 8+. While the solution undoubtedly works and leaves the main thread free for UI interactions, the memory usage climbs continuously so I am obviously doing something wrong. The class I have written is as follows: enum LongPollError:ErrorType{ case IncorrectlyFormattedUrl case HttpError } public class LongPollingRequest: NSObject { var GlobalUserInitiatedQueue: dispatch_queue_t { return dispatch_get_global_queue(Int(QOS_CLASS

Wait for an async methods to finish in a for loop

落花浮王杯 提交于 2019-12-21 01:21:46
问题 I have a for loop containing three asynchronous methods, and I want to make some treatment after this 3 async methods are finished. -(void)getAllUsersInformations{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for(User *user in users){ [self getUserInfo:user]; } //Here, I want to reload the table view for example, after finishing the for loop (executing the whole three methods). }); } -(void)getUserInfo:(User*)user{ [self getInformations:user]; [self

Core Data & GCD: Passing the correct managed object context to custom NSManagedObjects

冷暖自知 提交于 2019-12-20 17:26:12
问题 I get runtime errors which seem to result from my incorrect implementation of GCD in combination with my custom NSManagedObjects. Nested in a GCD call, I am using custom NSManagedObjects which (seem to) have their own managed object contexts (= self.managedObjectContext ). I am creating the managed object context in the app delegate by using the managed object context provided by UIManagedDocument : self.managedDocument.managedObjectContext . I don't understand how to pass the correct managed

Grand Central Dispatch (GCD) dispatch source flags

我们两清 提交于 2019-12-20 12:31:54
问题 I recently switched from using kqueue to GCD dispatch sources to monitor file changes. This has worked out well and resulted in a much simpler API. I documented my switch here. The only issue I have is that now I cannot access the flags on the event that I was able to in kqueue. For example with kqueue I was able to check if the file was deleted, renamed, or it's attributes were changed with the following: struct kevent event; ... if(event.flag & EV_DELETE) { printf("File was deleted\n"); }

Mulithreading: executing method calls only after finished executing other method

六眼飞鱼酱① 提交于 2019-12-20 10:56:06
问题 I am trying to process method asynchronously, as per requirements, once the first method has completed, only then the second method should start executing. The Problem is first method itself has code that runs on background thread. I tried dispatch_semaphore_wait, but that didnt work either. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ [self firstMethod]; NSLog(@

Good pattern for Internet requests with Grand Central Dispatch?

穿精又带淫゛_ 提交于 2019-12-20 10:37:02
问题 I'm currently using synchronous ASIHTTPRequest with GCD queues to download data from the Internet, then parse the response data with JSONKit. What do you think about this pattern. Thank you in advance. Here is my code: dispatch_async(queue, ^(void) { // Request is ASIHTTPRequest. [request startSynchronous]; // Parse JSON. NSArray *array = [[request responseData] objectFromJSONDataWithParseOptions:JKParseOptionLooseUnicode]; // Callback on the main queue to update UI. dispatch_async(dispatch

how to wrap an asynchronous method that takes a block and turn it synchronous in objective c

与世无争的帅哥 提交于 2019-12-20 09:59:18
问题 I want to wrap an async API that look like this: [someObject completeTaskWithCompletionHandler:^(NSString *result) { }]; into a synchronous method that i can call like this NSString *result = [someObject completeTaskSynchronously]; How do I do this? I did some doc reading and google search, and attempt to use "dispatch_semaphore" to do try to achieve it like so: -(NSString *) completeTaskSynchronously { __block NSString *returnResult; self.semaphore = dispatch_semaphore_create(0); [self

FIFO serial queue using GCD

岁酱吖の 提交于 2019-12-20 09:51:05
问题 I am trying to create a (network) synchronized array for the company I work for. While the networking part works fine, I have dwelled into an issue. My wish was to create a new queue using dispatch_create_queue , to which I would add two blocks that are not to run on the main thread, but in a serial manner, meaning that first the first block has to run, then the second, and never in parallel. I've read the apple documentation, but it is confusing to say the least. When I create my queue using

Running Python script from Cocoa application using GCD

…衆ロ難τιáo~ 提交于 2019-12-20 09:03:05
问题 I'm trying to run a Python script from a Cocoa app. It's working just fine on the main thread, but I'd like to have it running in the background, on a concurrent GCD queue. I'm using the following method to setup a manager class that runs the Python script: - (BOOL)setupPythonEnvironment { if (Py_IsInitialized()) return YES; Py_SetProgramName("/usr/bin/python"); Py_Initialize(); NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"MyScript" ofType:@"py"]; FILE *mainFile = fopen(