grand-central-dispatch

Does dispatch_after block the main thread?

梦想与她 提交于 2019-12-05 18:46:44
I'm setting a timer so that after a second passes I reset a value for my keyboard extension. The problem is that I feel like the following call is stalling my UI: dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [self resetDoubleTapBool]; }) Is there an asynchronous way of doing this, or a better way in general? Thanks! The dispatch_after() call itself does not block. At (or shortly after) the appointed time, the block will be submitted to the main queue. Submitting it doesn't block the main thread. When the main thread next runs its run loop or

Images Persistence and Lazy Loading Conflict With Dispatch_Async

瘦欲@ 提交于 2019-12-05 18:24:09
I am working on a feed reader and i am doing it by parsing rss feeds using nsxmlparser. I also have thumbnail objects that i am taking from the CDATA block. -(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding]; NSString *storyImageURL = [self getFirstImageUrl:someString]; NSURL *tempURL = [NSURL URLWithString:storyImageURL]; NSData *tempData = [NSData dataWithContentsOfURL:tempURL]; thumbnail = [UIImage

iOS - Unit Testing Asynchoronous code

佐手、 提交于 2019-12-05 16:29:33
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([GMGResultSetInfo class]); OCMStub([resultSet isLastPage]).andReturn(YES); OCMStub([self.mockParser resultSet])

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

大城市里の小女人 提交于 2019-12-05 15:18:55
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.halo.queue2", DISPATCH_QUEUE_SERIAL); for ( int i = 0; i < 10; i++) { dispatch_async(concurrencyQueue, ^(

iOS: dispatch_async and UIImageWriteToSavedPhotosAlbum

穿精又带淫゛_ 提交于 2019-12-05 15:04:47
Just learning how to allocate tasks among threads, or dispatch asynchronously. I understand that any operation that "touches" a view must be done on the main thread. What about: UIImageWriteToSavedPhotosAlbum ? I would assume this could be done on a background thread, but am I mistaken? Also, if it should be done on a background thread, is there a difference between these two calls below, as one saves a UIImage and the other saves a UIImage from a view? UIImageWriteToSavedPhotosAlbum(_someUIImage ,nil,nil,nil); UIImageWriteToSavedPhotosAlbum(_imageView.image ,nil,nil,nil); By the way I am

Swift 3 Parallel for/map Loop

人走茶凉 提交于 2019-12-05 13:36:37
There's quite a few threads on this however Using Grand Central Dispatch in Swift to parallelize and speed up “for" loops? uses Swift <3.0 code and I can't get the equivalent to work in 3 (see code). Process Array in parallel using GCD uses pointers and it gets a bit ugly so I'm going to assert right here that I'm looking for the nice Swift 3 way to do it (as efficiently as possible of course). I also heard groups are slow (?) maybe someone can confirm that. I couldn't get groups to work either. Here is my implementation of a striding parallel map function (in an extension of Array). It want's

Can't cancel executing operations in OperationQueue swift

被刻印的时光 ゝ 提交于 2019-12-05 12:06:15
Im doing some lengthy calculations to create chart data on a background thread i was originally use GCD, but every time a user filters the chart data by hitting a button, the chart data needs to be recalculated, if the user clicks the chart data filtering buttons very quickly (power user) then the chart loops through each drawing as each GCD dispatch async finishes I realize that I can't cancel threads with GCD so I've moved to trying to implement an OperationQueue I call cancelAllOperations() before adding a new operation to the queue The operations on the queue act funky, sometimes it seems

Is dispatch_once overkill inside of +[NSObject initialize]?

ぐ巨炮叔叔 提交于 2019-12-05 12:06:11
If I create a singleton inside of +[NSObject initialize] , do I need to put my code inside a dispatch_once block like so? static NSObject * Bar; @implementation Foo + (void)initialize { if (self == [Foo class]) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Bar = [NSObject new]; }); } } @end EDIT I'm concerned about this because I want to make sure that all threads will see that I've set Bar after +[Foo initialize] is called. The documentation says +[NSObject initialize] is thread-safe, but does that imply it is memory-safe? The answer to your direct question is that you don

NSExpression based Core Data fetch does not retrieve current values (iOS 5, GCD)

邮差的信 提交于 2019-12-05 09:59:10
What I am trying to do I am using the code below to download data (historic foreign exchange rates) from my backend server (parse.com) to my app's Core Data store. The app checks for the latest available data stored locally and fetches only the newer data from the server. If there is no data stored locally yet, it fetches all data from the server. The way the code is set up, it fetches the data in batches of 100 objects, saves the objects in Core Data, gets the new latest date for which data is now locally stored (by using NSExpression ) and fetches the next batch until no more new objects are

Barrier operations in NSOperationQueue

自古美人都是妖i 提交于 2019-12-05 09:08:07
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 provide(or atleast difficult) much access over the operations, like cancelling single operation, etc. Clay