grand-central-dispatch

Variable assigned in dispatch block coming back null

我的未来我决定 提交于 2019-12-02 06:49:49
I'm trying to make a network call run in a background thread so it doesn't freeze my app while it's waiting. The call happens when I do: nextTime = [myObj getNextTime]; I do an NSLog as soon as I get it, and that one works. However, when I'm outside of the dispatch block, the same NSLog prints out null. myObj *current = ((myObj *)sortedArray[i]); __block NSString *nextTime; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); dispatch_async(queue, ^{ nextTime = [myObj getNextTime]; NSLog(@"inside%@",nextTime); dispatch_sync(dispatch_get_main_queue(), ^{ });

for loop cycle showing always the last array value swift 3 after HTTP request

我的未来我决定 提交于 2019-12-02 06:40:18
I already used another post to solver part of the problem,so now I am able to wait the end of the request. I made a http request with Almofire and parse the resulting JSON file. the code inside the myGroup.notify i think is correctly executed ater the request is completed (if I print allCards.count returns the correct value). Why if I put the for loop inside it is always showing the last card? Thanks for any help import UIKit mport SwiftyJSON import Alamofire class ViewController: UIViewController { //variables Declaration var allCard = [Card]() let allCardsHTTP: String = "https://omgvamp

Waiting for multipart image sending get completed

霸气de小男生 提交于 2019-12-02 06:26:05
I'm impementing an application in iOS7, it's kind of a social network app with posts with images and a backend that saves all of the data sent form the client. The iOS client is sending the information of the post via json and after the info is sent, it starts to send the image via multipart form using AFNetworking . I need to be notified when the image is sent, so that I can refresh the main view of the app with the new posts, including the recently posted by the client. In the practice if I request the backend for the last posts and the multipart hasn't finished, the sending of the image

iOS - swift 3 - DispatchGroup

南笙酒味 提交于 2019-12-02 06:12:44
I created this basic architecture to handle my networking stuff, i wanted to keep it modular and structured: public class NetworkManager { public private(set) var queue: DispatchQueue = DispatchQueue(label: "com.example.app.dispatchgroups", attributes: .concurrent, target: .main) public private(set) var dispatchGroup: DispatchGroup = DispatchGroup() private static var sharedNetworkManager: NetworkManager = { let networkManager = NetworkManager() return networkManager }() private init() {} class func shared() -> NetworkManager { return sharedNetworkManager } public func getData() {

Can we update UI from background Thread?

≡放荡痞女 提交于 2019-12-02 05:32:13
Hello iOS experts just to clear my concept I have a bit confusion about UI updation from Main Thread. Apple requirements are that all UI related stuff should be carried out in main Thread.So to test: Case1: I dispatch a task to a global dispatch concurrent queue asynchronously . After some processing I update my UI stuff directly from the concurrent queue (background thread), working fine using the below code. dispatch_queue_t myGlobalQueue; myGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(myGlobalQueue, ^{ // Some processing // Update UI; }); Case2

Swift 3.0 unresolved identifier for DispatchQueue

你说的曾经没有我的故事 提交于 2019-12-02 04:38:33
问题 I have been trying to find the answer to this for hours now but still no avail. I am trying to use the following code: func fetchPosts() { ref.child("Amore").child("Posts").observeSingleEventOfType(.Value, withBlock: { (snapshot) in if let dictionary = snapshot.value as? [String: AnyObject] { let postMod = ReviewsPostModel() postMod.setValuesForKeysWithDictionary(dictionary) self.posts.insert(postMod, atIndex: 0) } DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { self.tableView

Passing an argument to dispatch_async

时间秒杀一切 提交于 2019-12-02 04:15:34
I'm new to Swift and looking at how the dispatch_async function works. The API doc shows dispatch_async having two parameters. However, I'm able to pass in one argument and it's okay. dispatch_async(dispatch_get_main_queue()) { } How come I don't need to pass in two arguments? Thank you, API Doc: It is a trailing closure syntax func someFunctionThatTakesAClosure(closure: () -> ()) { // function body goes here } // here's how you call this function without using a trailing closure: someFunctionThatTakesAClosure({ // closure's body goes here }) // here's how you call this function with a

How to send a message or notification to main thread in Xcode?

笑着哭i 提交于 2019-12-02 03:04:31
I have a function on iPad that I need to run 3 steps in sequence, let's say task1, task2, task3. Task2 needs load some data from server. So I need to put task2 into a separate background thread. - (IBAction)dbSizeButton:(id)sender { //Task1...... dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{ //Task2 .....which go to server and download some stuff and update database. dispatch_sync(dispatch_get_main_queue(), ^{ //Task3, continue to work on UI }); }); } But looks like what is happening is the app often got killed when Task2 starts

GCD : How to write and read to variable from two threads

感情迁移 提交于 2019-12-02 03:00:35
this may sound a newbie question anyway Im new to GCD, I'm creating and running these two following threads. The first one puts data into ivar mMutableArray and the second one reads from it. How do i lock and unclock the threads to avoid crashes and keep the code thread safe ? // Thread for writing data into mutable array dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); if (timer) { dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC *

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

百般思念 提交于 2019-12-02 02:49:31
问题 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