ios - Firebase - Save unique ID in a class variable and send its data to another controller - Swift -
i created unique id firebase using childbyautoid. able print id within reference function used create it, when assign value class variable , print in console comes out nil.
i trying send data view controller using override func prepareforsegue method. works when data string not work when variable holding uniqueidkey.
here code:
class mainviewcontroller: uiviewcontroller, cllocationmanagerdelegate{ var ref: firdatabasereference! var refhandle: uint! let locationmanager = cllocationmanager() let regionradius: cllocationdistance = 1000 var currentlocation: cllocation! var location: cllocationcoordinate2d! var latitude: double! var longitude: double! let geocoder = clgeocoder() var placemark: clplacemark? let date = nsdate() var currentorderidkey = "" @iboutlet weak var mapview: mkmapview! @iboutlet weak var useremaillabel: uilabel! @iboutlet weak var pickupaddress: uitextfield! @iboutlet weak var deliveryaddress: uitextfield! override func viewdidload() { ref = firdatabase.database().reference() locationmanager.delegate = self locationmanager.requestwheninuseauthorization() locationmanager.requestlocation() locationmanager.startupdatinglocation() locationmanager.desiredaccuracy = kcllocationaccuracybest refhandle = ref.observeeventtype(firdataeventtype.value, withblock: { (snapshot) in let datadict = snapshot.value as! [string: anyobject] print((datadict)) }) let userid: string = firauth.auth()!.currentuser!.uid ref.child("users").child(userid).observesingleeventoftype(.value, withblock: { (snapshot) in let useremail = snapshot.value!["email"] as! string self.useremaillabel.text = useremail }) super.viewdidload() print("\(currentlocation)") } func locationmanager(manager: cllocationmanager, didfailwitherror error: nserror) { print("didfailwitherror \(error)") if error.code == clerror.locationunknown.rawvalue { return } } func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) { currentlocation = locations.last! latitude = currentlocation.coordinate.latitude longitude = currentlocation.coordinate.longitude location = cllocationcoordinate2dmake(latitude, longitude) print("didupdatelocations \(currentlocation.coordinate)") if currentlocation.timestamp.timeintervalsincenow < -10 { return } let coordinateregion = mkcoordinateregionmakewithdistance(location, regionradius * 2.0, regionradius * 2.0) mapview.setregion(coordinateregion, animated: false) } @ibaction func requestpickupbutton(sender: anyobject) { ref = firdatabase.database().reference() let userid: string = firauth.auth()!.currentuser!.uid ref.child("users").child(userid).observesingleeventoftype(.value, withblock: { (snapshot) in let orderref = self.ref.child("users").child(userid).child("orders") let origin = self.pickupaddress.text! let destination = self.deliveryaddress.text! let orderid = orderref.childbyautoid() let formatter = nsdateformatter(); formatter.dateformat = "yyyy-mm-dd hh:mm:ss zzz"; let defaulttimezonestr = formatter.stringfromdate(self.date) let order = ["date": defaulttimezonestr, "origin": origin, "destination": destination] orderid.setvalue(order) self.currentorderidkey = orderid.key string print(self.currentorderidkey) ///this works! self.performseguewithidentifier("serviceconfirmation", sender: self) self.locationmanager.stopupdatinglocation() }) print(currentorderidkey) //this doesnt } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "serviceconfirmation" { let destinationconroller = segue.destinationviewcontroller as! uinavigationcontroller let targetcontroller = destinationconroller.topviewcontroller as! serviceconfirmationviewcontroller targetcontroller.currentorderidkey = "this works" //this not : targetcontroller.currentorderidkey = currentorderidkey } } @ibaction func signoutbutton(sender: anyobject) { try! firauth.auth()!.signout() if let storyboard = self.storyboard { let viewcontroller = storyboard.instantiateviewcontrollerwithidentifier("loginviewcontroller") self.presentviewcontroller(viewcontroller, animated: false, completion: nil) } } }
as bonus question, warning in console every time run app:
<uilayoutcontainerview: ...; frame = (0 0; 414 736); autoresize = w+h; gesturerecognizers = <nsarray: ....>; layer = <calayer: ....>>'s window not equal <uinavigationcontroller: ....>'s view's window!
thanks in advance!
this classic case of asynchrounous
calls, accessing value of currentorderidkey
in print(currentorderidkey)
before has been assigned.
your print(currentorderidkey)
line gets called before
self.currentorderidkey = orderid.key string print(self.currentorderidkey) ///this works!
gets called.
ref.child("users").child(userid).observesingleeventoftype(.value,..
sends asynchronous
call backend takes time retrieve data, before data retrieved print(currentorderidkey)
gets called resulting in null
instead of segueing through performsegue....
use instantiation:-
let secondscene = self.navigationcontroller?.storyboard?.instantiateviewcontrollerwithidentifier("serviceconfirmationviewcontrollervc_id") as! serviceconfirmationviewcontroller secondscene.valuetranfered = self. currentorderidkey self.navigationcontroller?.pushviewcontroller(secondscene, animated: true)
where serviceconfirmationviewcontrollervc_id secondscene storyboardid
might suggest reading this:- wikipedia : asynchronous calls & https://stackoverflow.com/a/748189/6297658
Comments
Post a Comment