completionhandler

How can I make a function complete before calling others in an IBAction?

爷,独闯天下 提交于 2019-12-06 01:41:14
I'm having trouble understanding completion handlers. I have a textFieldEditingDidChange IBAction that calls first a verify() function on the textfield input and then an if statement on the boolean returned by apply. The problem is that the if statement starts before verify() has finished . Here is the code: @IBOutlet weak var myTextField: UITextField! @IBAction func myTextFieldEditingDidChange(sender: AnyObject) { let yo = verify(myTextField.text!) print("\(yo)") // it always prints "true" because verify hasn't finished } func verify(myText: String) -> Bool { var result = true // some code

Resuming tasks using NSURLSession when app removed from background or on device reboot

自闭症网瘾萝莉.ら 提交于 2019-12-05 06:17:43
I have checked many docs but could not found solution for Resuming tasks using NSURLSession when app removed from background or on device reboot. I am dealing with amazon S3 to upload some files, In that I am able to Upload file to S3 using NSURLSessionUploadTask, when app is in foreground as well as in background. Resume task when app crashed due to any other reason while uploading and if app is not removed from background. Restart task if I reboot device while uploading and if app is not removed from background. Here is my code to achive resume fuctionality written in

Wait for completion handler to finish - Swift

你。 提交于 2019-12-05 00:18:59
问题 I am trying to check if UserNotifications are enabled and if not I want to throw an alert. So I have a function checkAvailability which checks multiple things, including the UserNotification authorization status. func checkAvailabilty() -> Bool { // // other checking // var isNotificationsEnabled = false UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in if granted { isNotificationsEnabled = true } else {

Firebase Swift 3 Completion handler Bool

一世执手 提交于 2019-12-04 21:40:58
I'm trying to write a completion handler for a function that checks if a user is a member of a team in firebase. I have a public class customFunctions in which I created a function ifUserIsMember . I seem to be a little stuck on the idea of completion handlers, and can't seem to figure out how to check the bool value on completion (if that makes sense). Here's my code for the class: import Foundation import GeoFire import FirebaseDatabase public class customFunctions { func ifUserIsMember(userid: String, completionHandler: @escaping (Bool) -> ()) { let ref = FIRDatabase.database().reference()

self refrence inside swift closure return nil some time

元气小坏坏 提交于 2019-12-04 19:48:14
I am accessing instance method inside closure in swift, self reference become nil in some cases which result crash my program. I tried to access using [weak self] but it failed to call the instance method when self is nil . [weak self] () -> () in The whole point of [weak self] is to not create a reference to self (probably to avoid circular links and memory leaks) so that it can be released. If that happens, then self will be nil . You should either not use [weak self] or, better yet probably, be prepared to handle the case of self having been released and set to nil . guard let strong = self

iOS 7 Completion handler never gets called

烈酒焚心 提交于 2019-12-04 14:33:18
In the following code none of the completion handlers ever get executed. The one explanation I was able to find is this Bug in iPhone Simulator 5.1 with Xcode 4.5 using UIManagedDocument . It says that it might be a bug in iPhone simulator 5.1 . However, I've tried iPhone simulators 6.0, 6.1 and 7.0, none of them worked. I'll really appreciate your help. (By the way, I've seen this code in the Stanford iOS course, lecture 14) NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@

Completion handler swift 3 return a variable from function

∥☆過路亽.° 提交于 2019-12-04 12:45:12
I am confused surrounding the syntax for a completion handler in swift 3. In the function below, after parsing an xml file from a web service call, it should return a variable (an array [String:String] ). My attempt is below, but obviously it is incorrect. enum HistoryKey { case success([String:String]) case failure(String) } private func getHistoryKeys(searchterm: String, completion: @escaping () -> HistoryKey) { let url = PubmedAPI.createEsearchURL(searchString: searchterm) let request = URLRequest.init(url: url as URL) let task = session.dataTask(with: request) { (data, response, error) in

Fetch data from Firebase by joining tables in iOS

依然范特西╮ 提交于 2019-12-04 12:01:54
问题 I am trying to fetch data from two different Firebase tables. Here is the structure of table: Post { 1{ pImages{ i1:true i2:true } } 2{ pImages{ i3:true } } } Images{ i1{ iUrl : .... pId : 1 } i2{ iUrl :... pId : 1 } i3{ iUrl:.... pId : 2 } } I need to retrieve images corresponding to post with id = 1. The following is my implementation to retrieve images: func retrieveImagesForPost(postId: String,completion: (result: AnyObject?, error: NSError?)->()){ var imgArray:[Image]=[] let postsRef =

How To Call a func within a Closure

六月ゝ 毕业季﹏ 提交于 2019-12-04 06:40:54
问题 In a model's class Location , I get the name of the current city: var currentLatitude: Double! var currentLongitude: Double! var currentLocation: String! var currentCity: String! func getLocationName() { let geoCoder = CLGeocoder() let location = CLLocation(latitude: currentLatitude, longitude: currentLongitude) geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in guard let addressDict = placemarks?[0].addressDictionary else { return } if let city = addressDict[

What's the most efficient way to refresh a tableView every time the app is pushed back to the foreground?

烂漫一生 提交于 2019-12-04 06:10:00
问题 Currently what I have is this: AppDelegate.applicationDidBecomeActive(): func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. guard let vc = self.window?.rootViewController?.children.first as! AlarmTableViewController? else { fatalError("Could not downcast rootViewController to type