grand-central-dispatch

GCD, NSThread, and performSelector:onThread: issues

梦想的初衷 提交于 2019-12-02 01:30:13
I'm attempting to debug some iOS crash logs that contain the following error message: *** Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[SomeClass performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform The relevant section of the code is: - (void) runInvocationOnMyThread:(NSInvocation*)invocation { NSThread* currentThread = [NSThread currentThread]; if (currentThread != myThread) { //call over to the correct thread [self performSelector:@selector(runInvocationOnMyThread:) onThread:myThread

How to capture a variable's current value for a block

大憨熊 提交于 2019-12-02 01:21:13
Is there a way to save the current value of a varible for later usage in a block? For example, for this Playground code: import UIKit import XCPlayground XCPlaygroundPage.currentPage.needsIndefiniteExecution = true class testClass { var i = 0 func test() { let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC)) * 5) dispatch_after(dispatchTime, dispatch_get_main_queue(), { self.test(self.i) }) i = 3 } func test(i: Int) { print("i: \(i)") } } let a = testClass() a.test() Is there a way to save the current value of i for dispatch_after in a way that I get

Dispatch Group inside NSOperation - Still allowing multiple operations despite maxConcurrentOperationCount = 1

喜夏-厌秋 提交于 2019-12-02 00:41:29
I am aiming for a serial download queue within NSOperation subclass using dispatch groups to manage async tasks. I have maxConcurrentOperationCount set to 1 i have defined my queue var GlobalDownloadQueue: DispatchQueue { return DispatchQueue.global(qos: DispatchQoS.QoSClass.background) } Then inside main() of NSOperation subclass i have defined my dispatch group, have 3 enters and 3 leaves, then a notify block. The notify block seems to be running at the right time, however there is still multiple instances of the NSOperation running at once. This is what the console is printing when i select

Why does dispatch_queue_create give an EXC_BAD_ACCESS error in Swift?

别来无恙 提交于 2019-12-01 22:24:54
问题 I am porting some code from C++ to Swift that used Grand Central Dispatch, and I am finding a curious error with dispatch_queue_create seemingly not working at all. For instance, in my C++ base class header, I would declare dispatch_queue_t m_WorkQ; and in the initializer, put m_ResultQ = dispatch_queue_create("com.myapp.mHitsUpdateQueue", 0); ... and everything was glorious. I've tried this in Swift, in my class, declaring this at class level: var resultQueue: dispatch_queue_t ... and in the

Enqueued from com.apple.main-thread (Thread 1) Crash | iOS | Swift 4.1

社会主义新天地 提交于 2019-12-01 21:35:34
问题 What's happening above: Initialize realm model from the received array of user data. Write all the models at once in the realm DB in the background thread. Realm model is getting updated if they already exist by creating a copy. Can anyone please guide me here about what I am doing wrong. 来源: https://stackoverflow.com/questions/55700998/enqueued-from-com-apple-main-thread-thread-1-crash-ios-swift-4-1

Why does dispatch_queue_create give an EXC_BAD_ACCESS error in Swift?

笑着哭i 提交于 2019-12-01 20:29:11
I am porting some code from C++ to Swift that used Grand Central Dispatch, and I am finding a curious error with dispatch_queue_create seemingly not working at all. For instance, in my C++ base class header, I would declare dispatch_queue_t m_WorkQ; and in the initializer, put m_ResultQ = dispatch_queue_create("com.myapp.mHitsUpdateQueue", 0); ... and everything was glorious. I've tried this in Swift, in my class, declaring this at class level: var resultQueue: dispatch_queue_t ... and in the initalizer, I have (among others) the line resultQueue = dispatch_queue_create("com.myapp

iOS GCD custom concurrent queue execution sequence

匆匆过客 提交于 2019-12-01 20:24:34
问题 I have question regarding this issue , According to Apple's documents Concurrent Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that are managed by the dispatch queue. The exact number of tasks executing at any given point is variable and depends on system conditions. In iOS 5 and later, you can create

Enqueued from com.apple.main-thread (Thread 1) Crash | iOS | Swift 4.1

我与影子孤独终老i 提交于 2019-12-01 19:45:21
What's happening above: Initialize realm model from the received array of user data. Write all the models at once in the realm DB in the background thread. Realm model is getting updated if they already exist by creating a copy. Can anyone please guide me here about what I am doing wrong. 来源: https://stackoverflow.com/questions/55700998/enqueued-from-com-apple-main-thread-thread-1-crash-ios-swift-4-1

EXC_BAD_INSTRUCTION happens when using dispatch_get_global_queue on ios 7(swift)

血红的双手。 提交于 2019-12-01 17:14:36
let downloadGroup = dispatch_group_create() var images = [UIImage]() var errors = [NSError]() dispatch_apply(UInt(urls.count), dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { (i) in dispatch_group_enter(downloadGroup) SimpleCache.sharedInstance.getImage(urls[Int(i)], completion: { (image, error) -> () in if let fullImage = image { images.append(fullImage) } else { if let err = error { DLog(err.description) errors.append(err) } } dispatch_group_leave(downloadGroup); }) } dispatch_group_notify(downloadGroup, dispatch_get_main_queue()) { completion(images, errors) } above is my code

Concurrent Queue with GCD? (iOS 4.2.1)

馋奶兔 提交于 2019-12-01 15:50:57
Iam having problems with: dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0UL); concurrentQueue is nil on iOS 4.2.1 (device) but the same code works perfectly on another device which is running iOS 5.0.1. When I checked the header it says it's available since iOS 4.0, am I doing something wrong? The code below fetches images from the internet and works great in everything after 4.2.1 but not in 4.2.1, any ideas why? Can you create a concurrent queue some other way using GCD? - (void)imageFromURL:(NSString*)link { if ([link length] == 0) return;