Swift - How to fetch next data if value changes - swift

I am trying to fetch next question from server after the previous one is played by users.
When arrayNum is changed, can't fetch the next question. I am thinking to use switch method to write same code over and over again. Is there a better way?
func retriveRoom(){
let idRef = db.collection("ROOMS_Collection").document(GameData.shared.documentID)
idRef.getDocument { (docSnapshot, error) in
guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
guard let data = docSnapshot.data() else { return }
let questionID = data["questionID"] as? Array<Int>
let questionRef = self.db.collection("QUESTIONS_Collection").document("\(questionID![self.arrayNum])")
questionRef.getDocument{ (docSnapshot, error) in
guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }
let data = docSnapshot.data()
self.questionArray = (docSnapshot.data()?.compactMap({_ in Question(dictionary: data!)}))!
let question = self.questionArray[0]
self.answer = Int(question.answer)!
self.questionText.fontColor = UIColor.white
self.questionText.fontName = "Verdana"
self.questionText.text = ("\(question.question)")
self.questionText.fontSize = 24.0
self.questionText.numberOfLines = 0
self.questionText.lineBreakMode = NSLineBreakMode.byWordWrapping
self.questionText.zPosition = 5
self.questionText.position = CGPoint(x: (self.questionBackground?.frame.midX)! - 5, y: (self.questionBackground?.frame.midY)! - 5)
self.questionText.preferredMaxLayoutWidth = (self.questionBackground?.frame.width)! - 10
self.addChild(self.questionText)
}
}
}

If I understand what you are trying to do correctly, you want to fetch another question from your database when arrayNum changes. I think your best bet is to try a property observer function on your arrayNum variable. Something like this:
In your class declaration where you declare arrayNum:
var arrayNum : Int {
didSet {
// Implement retrieveRoom() here
retrieveRoom()
}
}
What is not clear from your code is exactly when and how arrayNum changes. As an alternative to didSet you can also use willSet, which, as the name suggests, is called when a parameter value is about to be reassigned. You can read up on this here:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html
(And by the way, it might serve your user experience better if you were to fetch a batch of questions in one go rather than repeat the database call every time a new question is required. But that is not strictly relevant to your question.)
Hope that helps.

Related

save increase int as multiple core data entities

In my swift code below I am trying to save ints to core data. Every time a user hits a button a new int is created. So if the user hits the button twice there are know 2 int entities in core data. My code below is having a runtime error and I dont know how to solve it.
pic
var pageNumber = 0
var itemName : [NSManagedObject] = []
func enterData() {
let appDeldeaget = UIApplication.shared.delegate as! AppDelegate
let context = appDeldeaget.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Player", in: context)
let theTitle = NSManagedObject(entity: entity!, insertInto: context)
theTitle.setValue(pageNumber, forKey: "positon")
do {
try context.save()
itemName.append(theTitle)
pageNumber += 1
}
catch {
}
self.theScores.reloadData()
positionTextField.text = ""
positionTextField.resignFirstResponder()
}
You are introducing a few complications that might be causing the issue.
First, if I understood your purpose, the itemName should not be an array of NSManagedObject, but rather an array of Player. Also, creating the theTitle can be simplified.
Try this instead of the code you proposed:
var pageNumber = 0
// If I understand correctly, you should have an array of Players
var itemName : [Player] = []
func enterData() {
let appDeldeaget = UIApplication.shared.delegate as! AppDelegate
let context = appDeldeaget.persistentContainer.viewContext
// Simpler way to create a new Core Data object
let theTitle = Player(context: context)
// Simpler way to set the position attribute
theTitle.position = pageNumber // pageNumber must be of type Int64, otherwise use Int64(pageNumber)
do {
try context.save()
itemName.append(theTitle)
pageNumber += 1
} catch {
// handle errors
}
// rest of the code
}

How can I wait data from firebase?

I need to wait data from firebase otherwise my app gonna crash because of nil value. I searched about that and watch some videos and look at other questions but I can't see any firebase example actually. How can I wait data from firebase? How can I use dispatch or semaphore and which one is more efficient?
Here my sample code:
let uid = (Auth.auth().currentUser?.uid)!
let usrIndirimRef = Database.database().reference().child("users").child(uid).child("indirimSayisi")
usrIndirimRef.observe(.value) { (snap) in
let userIndir = snap.value as! Int
self.userIndirimSayisi = userIndir
let usrIndirimTipiRef = Database.database().reference().child("users").child(uid).child("indirimTipi")
usrIndirimTipiRef.observe(.value) { (snapshot) in
if userIndir < 1 {
self.userIndirim = "0"
self.indirimKontrol = false
self.tableView.reloadSections([1], with: .fade)
}else{
self.userIndirim = snapshot.value! as! String
self.tableView.reloadSections([1], with: .fade)
}
}
}
toplamHesapTutari()
Here toplamHesapTutari() function execute before the data userIndirim. Its can be solve maybe hold execution for that function but I can't decide that is correct way.
Don't wait, never wait, run the code you need to run inside the completion handler
let uid = (Auth.auth().currentUser?.uid)!
let usrIndirimRef = Database.database().reference().child("users").child(uid).child("indirimSayisi")
usrIndirimRef.observe(.value) { (snap) in
let userIndir = snap.value as! Int
self.userIndirimSayisi = userIndir
let usrIndirimTipiRef = Database.database().reference().child("users").child(uid).child("indirimTipi")
usrIndirimTipiRef.observe(.value) { (snapshot) in
if userIndir < 1 {
self.userIndirim = "0"
self.indirimKontrol = false
self.tableView.reloadSections([1], with: .fade)
}else{
self.userIndirim = snapshot.value! as! String
self.tableView.reloadSections([1], with: .fade)
}
self.toplamHesapTutari()
}
}
I had this problem before and I tried a lot of methods but still crash my app.
So I figure it out by putting the code that makes my app crash in a later function to spare some time for the essential data fully loads. I suggest you use try-catch and log to observe the error and prevent your app from keep crashing.
try {
if ((snapshot.child("photo").value as? String)==null) {
//if not load
} else {
//if load
}
}catch(ex: Exception){}
Here is some reference for you:
https://kotlinlang.org/docs/reference/exceptions.html

Firebase don't send me my value into my variable

I've got a code which normally should return to me a value from Firebase.
My Firebase struct is :
Experience{
UserId{
LDG_DAY: "4"
LDG_NIGHT: "0"
APCH_IFR: "0"
}
}
My code is :
func getUserExp(){
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
let Date = self.flightDate.text
ref.child("Experience")/*.child(userID!)*/.observeSingleEvent(of: .value) {(snapshot) in
if snapshot.hasChild(userID!){
let value = snapshot.value as? NSDictionary
let ldg_day = value?["LDG_DAY"] as? String ?? "123"
let ldg_night = value?["LDG_NIGHT"] as? String ?? "0"
let apch_ifr = value?["APCH_IFR"] as? String ?? "0"
self.intLdgDay = Int(ldg_day)!
self.intLdgNight = Int(ldg_night)!
self.intApchIfr = Int(apch_ifr)!
print("string = \(ldg_day) int = \(self.intLdgDay)")
}
}
}
Now the code didn't work as I would like... In fact my code return the basic as? String ?? "123" value but the snapshot.value get the good value from firebase ...
What's wrong ? I use this code for many other part of my app and no problems about it ?
Thanks for your help
I believe you want to ensure the node exists before trying to read the child data.
NOTE:
I see the path to read has the uid commented out so it's unclear if you intended to read a single user (leaving in the uid) or if you actually wanted to load every user at one time (thousands). This answer assumes you are intending to read that specific user node only. See #Callam answer if you intended to read ALL of the users nodes at one time.
The code you have now is using snapshot.hasChild which looks within the node to see if the child, the users uid exists, and it doesn't so the code will always fail.
if snapshot.hasChild(userID!)
I think what you want to do is use snapshot.exists to ensure it's a valid node before reading. Here's the code:
let experienceRef = self.ref.child("Experience")
let usersExpRef = experienceRef.child(uid)
usersExpRef.observeSingleEvent(of: .value) { snapshot in
if snapshot.exists() {
let value = snapshot.value as! [String: Any]
let ldg_day = value["LDG_DAY"] as? String ?? "123"
print("string = \(ldg_day)")
} else {
print("the \(uid) node does not exist")
}
}
I would also suggest safely unwrapping options before attempting to work with them as they could be nil, and that would crash your code.
guard let thisUser = Auth.auth().currentUser else { return }
let uid = thisUser.uid
Note I also replaced the old objc NSDictionary with it's Swifty counterpart [String: Any]
Assuming your struct is from the root, and Experience contains more than one user ID, your code is currently observing the value for all user IDs since the /*.child(userID!)*/ is commented out.
Therefore you are requesting every user's experience and checking on the client if the current user exists as a child – this will succeed if the current user's ID is present at Experience/$uid.
ref.child("Experience")/*.child(userID!)*/.observeSingleEvent(of: .value) { (snapshot) in
if snapshot.hasChild(userID!) {
let value = snapshot.value as? NSDictionary
Now we have a snapshot with all Experiences and we've confirmed that it has a child for the current user's ID – we would need to get that child and cast the value of that to a dictionary.
let value = snapshot.childSnapshot(forPath: userID).value as? NSDictionary
This fixes the issue but obviously, we don't want to download every experience on a single user's device, and they maybe shouldn't even have the permission to request that reference location either.
So if you uncomment .child(userID!), the snapshot will be of just one Experience, so snapshot.hasChild(userID!) will fail. Instead, you can use snapshot.exists() and/or a conditional cast to determine if the snapshot for the userID is existent and/or thereby castable.
func getUserExp() {
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
let Date = self.flightDate.text
ref.child("Experience").child(userID!).observeSingleEvent(of: .value) { snapshot in
if snapshot.exists() {
let value = snapshot.value as? [String:String]
let ldg_day = value?["LDG_DAY"] ?? "123"
let ldg_night = value?["LDG_NIGHT"] ?? "0"
let apch_ifr = value?["APCH_IFR"] ?? "0"
self?.intLdgDay = Int(ldg_day)!
self?.intLdgNight = Int(ldg_night)!
self?.intApchIfr = Int(apch_ifr)!
print("string = \(ldg_day) int = \(self.intLdgDay)")
} else {
print("experience for \(snapshot.key) doesn't exist")
}
}
}
You can clean this up a bit with a struct and extension.
// Experience.swift
struct Experience {
var ldg_day: String
var ldg_night: String
var apch_ifr: String
}
extension Experience {
static var currentUserRef: DatabaseReference? {
return Auth.auth().currentUser.flatMap {
return Database.database().reference(withPath: "Experience/\($0.uid)")
}
}
init?(snapshot: DataSnapshot) {
guard snapshot.exists() else { return nil }
let value = snapshot.value as? [String:String]
self.ldg_day = value?["LDG_DAY"] ?? "123"
self.ldg_night = value?["LDG_NIGHT"] ?? "0"
self.apch_ifr = value?["APCH_IFR"] ?? "0"
}
}
Et voilà,
func getUserExp() {
Experience.currentUserRef?.observeSingleEvent(of: .value, with: { [weak self] in
if let experience = Experience(snapshot: $0) {
self?.intLdgDay = Int(experience.ldg_day)!
self?.intLdgNight = Int(experience.ldg_night)!
self?.intApchIfr = Int(experience.apch_ifr)!
print("string = \(experience.ldg_day) int = \(self.intLdgDay)")
} else {
print("experience for \($0.key) doesn't exist")
}
})
}

Can't extract var from firebase reading method

How can I get the numberOfMarkers out of the reading method of Firebase in Swift?
if I use the function in the {} this will save and I will be can use it not in the {}?
docRef = Firestore.firestore().document("Markol/Markers")
docRef.getDocument{ (docSnapshot, error) in
guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
let myData = docSnapshot.data()
let numberOfMarkers = myData?["NumberofMarkers"] as? Int ?? 0
}
//Here i want to get the let numberOfMarkers
var markerArrayList = [GMSMarker]()
func makeAMarker(_ Latitude:Double , _ Longitude:Double , _ Title:String,Snippet:String) -> GMSMarker{
let GmMarker = GMSMarker()
GmMarker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(Latitude), longitude: CLLocationDegrees(Longitude))
GmMarker.title = Title
GmMarker.snippet = Snippet
GmMarker.icon = UIImage(named: "smallStoreIcon")
return GmMarker
}
getDocument is an asynchronous task, so numberOfMarkers is only accessible before the closing }.
Do whatever you want with numberOfMarkers inside the getDocument listener, you may need to refactor your existing code to accommodate this. For example:
docRef = Firestore.firestore().document("Markol/Markers")
docRef.getDocument{ (docSnapshot, error) in
guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
let myData = docSnapshot.data()
let numberOfMarkers = myData?["NumberofMarkers"] as? Int ?? 0
processMarkers(numberOfMarkers, myData)
}
If this approach isn't clear, try posting more of your code in your question so others can help you restructure.
No you can't. Variable/constant is always visible just inside scope where is declared, between curly braces {...}.
What you probably want to do is to get this value to return it or use somewhere else. Don't do it, since getting data from Firestore is asynchronus task, use completion handler instead and return value (or nil if you don’t have value) as completion's parameter when you have it
func call(completion: #escaping (Int?) -> Void) {
...
docRef.getDocument{ docSnapshot, error in
guard let docSnapshot = docSnapshot, docSnapshot.exists else {
completion(nil)
return
}
let myData = docSnapshot.data()
let numberOfMarkers = myData?["NumberofMarkers"] as? Int
completion(numberOfMarkers)
}
}
then when you need to call it
call { numberOfMarkers in // code inside this closure is called with parameter of type `Int?` when you receive data and call completion from inside `call`
if let number = numberOfMarkers {
... // do something with it
}
}
... here you can use it for next purpose

How to reduce if-condition looping - Swift

I know it sounds crazy, but just curious how I can reduce the if loop iteration for following? I have tried using guard let but stucked at some place.
{
if arenaEventItems == nil || arenaEventItems.count <= 0 {
return
}
if (arenaEventItems.count > 0 && (self.arenaEvents?.monthsDictObjList.count)! > 0){
if (self.tableView != nil){
if let arrVisibleRows = self.tableView.indexPathsForVisibleRows as? [IndexPath]{
if (self.tableView.indexPathsForVisibleRows!.count > 0){
let indexPath : IndexPath = self.tableView.indexPathsForVisibleRows!.first!
if let dict = self.arenaEvents?.monthsDictObjList[indexPath.row] {
if(self.arenaHeaderView != nil) && (dict.count) > 0 {
self.arenaHeaderView?.setMonthTitle(string: (dict.keys.first!))
let selectedMonthTitle = (dict.keys.first!)
for month in (self.arenaEvents?.uniqueMonthOnlyList)! {
if (selectedMonthTitle.contains(month)){
selectedMonthIndex = (self.arenaEvents?.uniqueMonthOnlyList.index(of: month)!)!
break
}
}
}
}
}
}
}
}
}
You can reduce it like that, without any forced unwrapping or nesting:
guard let arenaEventItems = arenaEventItems,
!arenaEventItems.isEmpty,
let arenaEvents = self.arenaEvents,
!arenaEvents.monthsDictObjList.isEmpty,
let arenaHeaderView = self.arenaHeaderView,
let indexPath = self.tableView?.indexPathsForVisibleRows?.first,
let selectedMonthTitle = arenaEvents.monthsDictObjList[indexPath.row].keys.first
else {
return
}
arenaHeaderView.setMonthTitle(string: selectedMonthTitle)
if let monthIndex = arenaEvents.uniqueMonthOnlyList.index(where: { selectedMonthTitle.contains($0) }) {
selectedMonthIndex = monthIndex
}
you replace if ... return with guard !... else return to avoid nesting
you replace .count > 0 with !...isEmpty as best practice
you replace multiple access to self.something? with if let something = self.something to avoid threading issues
you unloop for ... in ... { if (...) { ... } } to .index(where: ...)
You can combine all the conditions in "if" and get something like this:
if let eventItems = arenaEventItems,
eventItems.count > 0,
let events = self.arenaEvents,
!events.monthsDictObjList.isEmpty,
let tableView = self.tableView,
let arrVisibleRows = self.tableView.indexPathsForVisibleRows as? [IndexPath],
!arrVisibleRows.isEmpty,
let indexPath : IndexPath = arrVisibleRows.first,
let dict = events.monthsDictObjList[indexPath.row],
let headerView = self.arenaHeaderView,
!dict.isEmpty {
headerView.setMonthTitle(string: (dict.keys.first!))
let selectedMonthTitle = (dict.keys.first!)
for month in events.uniqueMonthOnlyList! {
if (selectedMonthTitle.contains(month)){
selectedMonthIndex = (events.uniqueMonthOnlyList.index(of: month)!)!
break
}
}
}
You should consider restructuring your code, your code is not readable and incomprehensible for anyone who look at it. Since, you are using Swift, it is really easy to write such code with guard ... else, if ... let
pattern.
Some improvements that you can do on class is have your view non nil ie make them implicitly unwrapped optional, since you will always be connecting them to storyboard.
#IBOutlet var tableView: UITableView!
#IBOutlet var arenaHeaderView: ArenaHeaderView!
Also, you have arrays which can go to nil, why do you want it to be nil. You could simply initialize an empty array and dictionaries. That way you can reduce some more comparison code like so,
arenaEventItems: [String: String] = [:]
With that changes and a bit of refactoring, you could possibly rewrite your code to something like this,
guard !arenaEventItems.isEmpty,
let arenaEvents = arenaEvents,
let indexPath = tableView.indexPathsForVisibleRows?.first,
let dict = arenaEvents.monthsDictObjList[indexPath.row],
let selectedMonthTitle = dict.keys.first
else {
return
}
arenaHeaderView.setMonthTitle(string: selectedMonthTitle)
for month in arenaEvents.uniqueMonthOnlyList where selectedMonthTitle.contains(month) {
if let selectedIndex = arenaEvents.uniqueMonthOnlyList.index(of: month) {
selectedMonthIndex = selectedIndex
break
}
}