grand-central-dispatch

dispatch_write() and dispatch_read() usage

我的梦境 提交于 2019-12-07 11:33:58
问题 I'm just playing with some GCD functions for writing and reading data to files. Two of these functions are dispatch_write() and dispatch_read() , which allow one to write and read data to a file descriptor without having to setup a new dispatch_io_t channel. So, I have the following code: #import <dispatch/dispatch.h> #import <stdio.h> #import <unistd.h> int main() { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); int intbuffer[] = { 1, 2, 3, 4 };

iOS - Unit Testing Asynchoronous code

对着背影说爱祢 提交于 2019-12-07 10:28:51
问题 The part of a method that I am trying to test is as follows: - (void)configureTableFooterView { dispatch_async(dispatch_get_main_queue(), ^{ self.tableView.tableFooterView = nil; if ([self.parser.resultSet isLastPage]) { return; } }); } I have written the unit test as follows: - (void)testTableFooterViewConfigurationAfterLastPageLoaded { id mockTableView = OCMClassMock([GMGFlatTableView class]); OCMExpect([mockTableView setTableFooterView:[OCMArg isNil]]); id resultSet = OCMClassMock(

main thread does dispatch_async on a concurrent queue in viewDidLoad, or within a method matters

北城以北 提交于 2019-12-07 09:40:04
问题 So with some help, I am more clear on how a nested GCD works in my program. The original post is at: Making sure I'm explaining nested GCD correctly However, you don't need to go through the original post, but basically the code here runs database execution in the background and the UI is responsive: -(void)viewDidLoad { dispatch_queue_t concurrencyQueue = dispatch_queue_create("com.epam.halo.queue", DISPATCH_QUEUE_CONCURRENT); dispatch_queue_t serialQueue = dispatch_queue_create("com.epam

Adding to array in parallel

孤街醉人 提交于 2019-12-07 08:12:32
问题 I'm using Grand Central Dispatch to transforms elements of one array into another. I call dispatch_apply on the source array, transform it into zero or more items, then add them to the destination array. This is a simplified example: let src = Array(0..<1000) var dst = [UInt32]() let queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT) dispatch_apply(src.count, queue) { i in dst.append(arc4random_uniform(UInt32(i))) // <-- potential error here } print(dst) I sometimes get an

What's the harm of retain self in block?(Objective-C, GCD)

青春壹個敷衍的年華 提交于 2019-12-07 08:04:08
问题 In many guide about how to use blocks and GCD, one tip is always mentioned : do not retain self in block. The detail is when defining a block, if you reference self or a ivar of self, then self is retained by the block. So the work around is to use __block modifier to get a weakSelf or weakIvar . But what's the harm of not doing that? If the block retains self, it should release self when the block is finished(Am I right about this?). So ultimately the reference count of self is balanced. I

GCD obtaining queue name/label

こ雲淡風輕ζ 提交于 2019-12-07 06:53:40
问题 How can I get the current queue name? I mean queue label like com.example.myqueue . In the Xcode 4 debugger I can see just _block_invoke_1 . 回答1: How about dispatch_queue_get_label? 回答2: In Objective-C you can log the label of the current queue with: NSLog(@"%s", dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)); In Swift (2.0): print(String(UTF8String: dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL))!) 来源: https://stackoverflow.com/questions/5166711/gcd-obtaining-queue-name

Why is my computer not showing a speedup when I use parallel code?

陌路散爱 提交于 2019-12-07 02:15:22
问题 So I realize this question sounds stupid (and yes I am using a dual core), but I have tried two different libraries (Grand Central Dispatch and OpenMP), and when using clock() to time the code with and without the lines that make it parallel, the speed is the same. (for the record they were both using their own form of parallel for). They report being run on different threads, but perhaps they are running on the same core? Is there any way to check? (Both libraries are for C, I'm

Barrier operations in NSOperationQueue

筅森魡賤 提交于 2019-12-07 02:03:58
问题 How can we implement dispatch_barrier_async 's equivalent behavior using NSOperationQueue or any user-defined data-structure based on NSOperationQueue ? The requirement is, whenever a barrier operation is submitted it should wait until all non-barrier operations submitted earlier finish their execution and blocks other operations submitted after that. Non-barrier operations should be able to perform concurrently. Barrier operations should execute serially. NB: Not using GCD ,as it doesn't

launchd: sleep in GCD managed signal handler

给你一囗甜甜゛ 提交于 2019-12-06 19:36:29
I encounter a strange situation in a launchd managed daemon when I try to sleep in the SIGTERM handler that is managed with Grand Central Dispatch as described here . Everything works fine and I do get a SIGTERM signal handler before receiving a SIGKILL when I do not sleep in the SIGTERM handler. But as soon as I do sleep -- even for extremly short amounts of time like a usleep(1); -- I do not get a SIGTERM handler at all but instead my daemon is SIGKILLed instantly by launchd. Btw I am using EnableTransactions in my plist file and the proper vproc_transaction_begin(3) / vproc_transaction_end

iOS stop global queue from running

微笑、不失礼 提交于 2019-12-06 16:49:55
Code: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ [ServerAPI API_GetChatList:self withUserId:[self getUserIDFromUserDefaults] withScheduleId:strGroupId withType:@"group"]; dispatch_async(dispatch_get_main_queue(), ^{ [self performSelector:@selector(getChatList) withObject:nil afterDelay:10]; }); }); I used dispatch global queue to call a method for every 10 seconds.It is working fine.The problem is global queue is keep running in other controllers too.How do i stop this from running?any help will be appreciated. You can keep a BOOL property,before every