Kevin Maldjian
Kevin Maldjian

Reputation: 147

Asynchronous Swift call into array

Currently I am attempting to push values into an array of note objects from firebase The only issue is due to Firebases asynchronous nature, I am having trouble getting the main thread to wait until the fetch function is completed. I have viewed many answers on this site and I have read up on the Semaphores and Dispatch queue documentation however I cannot get this fetch to work. It appears that most of the people here are attempting to use a table view which I am not.

Here is the fetch code

func fetchUser(){


FIRDatabase.database().reference().child("notes").observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = noteClass(dictionary: dictionary)
            self.coordinotes.append(user)
        }
    }, withCancel: nil)
} 

I have removed all of my semaphore and dispatch main attempts due to none of them working. This function is called in my view did load. When i check the values of my array that i push them into 'coordinotes' the value is not yet placed in and i get an out of bounds error.

Rest of code

import UIKit
import MapKit
import CoreLocation
import Firebase
import FirebaseDatabase

struct PreferencesKeys{
    static let savedItems = "savedItems"
}




class ViewController: UIViewController, CLLocationManagerDelegate{

    let manager = CLLocationManager()
    var coordinotes:[noteClass] = Array()
    var latitude = Double()
    var noteTime = noteBrain()

    //Map
    @IBOutlet weak var map: MKMapView!


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations[0]
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, noteTime.span)
        map.setRegion(region, animated: true)
        self.map.showsUserLocation = true
    }


    override func viewDidLoad()
    {
        super.viewDidLoad()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))

        if FIRAuth.auth()?.currentUser?.uid == nil {
            perform(#selector(handleLogout), with: nil, afterDelay: 0)
        }
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        fetchUser()
        loadAllCoordinotes()
    }

    func handleLogout() {

        do {
            try FIRAuth.auth()?.signOut()
        } catch let logoutError {
            print(logoutError)
        }

        let loginController = LoginController()
        present(loginController, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


func loadAllCoordinotes() {
    let length = coordinotes.count - 1
    map.addAnnotation(coordinotes[length])
}

Upvotes: 0

Views: 469

Answers (1)

Mat
Mat

Reputation: 6324

func fetchUser(_ completion:@escaping ([noteClass] , _ success: Bool)-> Void){
    let coordinotes = [noteClass]() 

FIRDatabase.database().reference().child("notes").observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = noteClass(dictionary: dictionary)
            coordinotes.append(user)
        }
        completion(coordinotes, true)
    }, withCancel: nil)

}

and then you call it in viewDidLoad like this:

  fetchUser { (coordinotes, success) in
            if success {
                self.coordinotes = coordinote
                self.loadAllCoordinotes()
            }
        }

Upvotes: 1

Related Questions