Swift - Firebase multiple snapshots - database

So I need to capture the contents from different paths in my firebase database and save them into an array so that I can put them into a chart.
Is there any way I can get the snapshot.Value and save it into a variable that can then be accessed at a later point in the code?
This is the code which shows what I'm trying to do:
override func viewDidLoad() {
super.viewDidLoad()
leave.observeEventType(.Value, withBlock: { snapshot in
let vaiable = snapshot.value
})
remain.observeEventType(.Value, withBlock: { snapshot in
let variableTwo = snapshot.value
})
undecided.observeEventType(.Value, withBlock: { snapshot in
let vaiableThree = snapshot.value
})
let options = ["Remain", "Leave", "Undecided"]
let results = [variable, variableTwo, variableThree]
setChart(options, values: results)
But I believe that the firebase calls are made last of all, meaning that the variables are empty in the 'results' array.
Anything I'm missing?

First of all, you've got an issue with 'scope'. A variable declared inside of a function or a callback will never be accessible from the outside of that function or callback.
One solution to your problem could be like this:
var results: [FIRDataSnapshot] = []
override func viewDidLoad() {
super.viewDidLoad()
leave.observeEventType(.Value, withBlock: { snapshot in
self.results.append(snapshot.value)
})
remain.observeEventType(.Value, withBlock: { snapshot in
self.results.append(snapshot.value)
})
undecided.observeEventType(.Value, withBlock: { snapshot in
self.results.append(snapshot.value)
})
But now you don't really have a way of knowing wether or not your results array is actually populated with any data, or if the callbacks are still to complete.
A safer, although still a bit cumbersome way to do it could be to refactor your callbacks out into seperate functions, and then when your first callback completes, it calls the function for the second callback, which then call the function for the third callback and then you can do something with your data. Hope that makes sense

Related

Updating array value using find object in the background is swift

I'm trying to retrieve data from Parse using findObjectsInbackground and store it in an array. I've already created outside the scoop of viewDidLoad().
I managed retrieving and printing the data, but they are not stored in the array and it keeps being empty!
I used self.articalsTableView.reloadData() but unfortunately the array myArray is still empty.
Any help please, this has been confusing me for two days!
import UIKit
import Parse
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var articalsTableView: UITableView!
var myArray = [PFObject]()
override func viewDidLoad() {
super.viewDidLoad()
let query = PFQuery(className: "Gateshead")
query.findObjectsInBackground {(objects, error) -> Void in
if error != nil {
print(error)
} else {
if let articals = objects {
for artical in articals {
// print (articles) THIS IS WOKING WELL
self.myArray.append(artical)
self.articalsTableView.reloadData()
}
}
}
}
}
}
findObjectsInback is using a background thread and you should dispatch to mainThread whenever if you want to access UIKit stuff in this case updating self.articalsTableView.
Also self.articalsTableView.reloadData() should be called at the end (not in the loop). This to prevent a race condition on self.myArray being accessed by the main-thread (to update self.articalsTableView ) and by the background-thread (to append artical)
for artical in articals {
// print (articles) THIS IS WOKING WELL
self.myArray.append(artical)
}
DispatchQueue.main.async {
self.articalsTableView.reloadData()
}

How to pass data into swiftui view and access it

Im trying to pass data into a swiftui view to be displayed when it is initialized, but am having trouble getting it to work.
Here is my code which passes data into my 'MarkerInfoWindow.swift' view:
func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
print("Showing marker infowindow")
print("marker.userData: \(marker.userData)")
let mUserData = marker.userData as? [String:String]
print("mUserData?['name']", (mUserData?["name"]) ?? "mUserData[name] was nil")
let mInfoWindow = UIHostingController(rootView: MarkerInfoWindow(placedata: mUserData!))
return mInfoWindow.view
}
Here is the code to my 'MarkerInfoWindow.swift' view:
struct PlaceDataStruct {
var name : String
var place_id : String
}
struct MarkerInfoWindow: View {
var placedata: [PlaceDataStruct]
var body: some View {
//Here is where i keep getting errors regardless if i use this method or dot notation
Text(placedata["name"])
}
}
Im not sure if im implementing my PlaceDataStruct incorrectly.
Does anyone know what I'm doing wrong, so that I can display the right data every time my view is initialized?
You’re MarkerInfoWindow is expecting an array of structs but the data you are passing is a dictionary, in the form [String: String]
You should update your MarkerInfoWindow to accept a dictionary instead. Here is an example of how to do that.
struct MarkerInfoWindow: View {
var placedata: [String: String] // update to be dictionary
var body: some View {
Text(placedata["name", default: ""]) // use correct notation for accessing dictionary
}
}
You’re also force unwrapping the data before you pass it to your MarkerInfoWindow if that’s your intention that is ok. But note that if your data isn’t there then it will crash your app.

Is it possible to make array empty without activating its DidSet property?

Currently I have an array that activates another function when set:
var updatedBeaconDetailsArray = [BeaconDataDetails]() { didSet {
self.updateBeaconData(beacon: self.updatedBeaconDetailsArray)
}
}
I'm trying to clear the array with activating the didSet since the didSet function is also tied to other parts of the app causing odd behavior.
So far I've tried:
self.updatedBeaconDetailsArray = []
self.updatedBeaconDetailsArray.removeAll()
but they all still activate the didSet, any thoughts?
What if you checked to see if the array is empty inside of the didSet?
Something like:
var updatedBeaconDetailsArray = [BeaconDataDetails]() {
didSet {
if !updatedBeaconDetailsArray.isEmpty {
self.updateBeaconData(beacon: self.updatedBeaconDetailsArray)
}
}
}

Swift: trouble with Arrays of classes

I'm trying to create an array of two items: a URL and a String. The index of the items is of significance.
The data is taken from QuartzFilterManager, which provides information about the Quartz Filters installed on a system.
import Cocoa
import Quartz
class myFilter {
var myURL: URL?
var name: String = ""
}
func getFilters() -> Array<String> {
var filterArray: Array<myFilter>
if let Filters = QuartzFilterManager.filters(inDomains: nil) {
for (index, eachFilter) in Filters.enumerated() {
filterArray[index].name.append((eachFilter as! QuartzFilter).localizedName()!)
filterArray[index].myURL!.append((eachFilter as! QuartzFilter).url()!)
}
}
}
Xcode complains about Type URL not having an append method. But the name property in the preceding line works. In short, how do I set specific properties in my array?
There are lots of issues.
Make myFilter a struct instead of class and name it properly as MyFilter.
You never initialize your filterArray, you only declare it.
You need to create a new instance of MyFilter for each url/name pair you want to add to the array.
You don't need to use enumerated in this case.
You have the wrong return type for your getFilter function (I think).
Use proper naming conventions.
Here's cleaned up code:
struct MyFilter {
var myURL: URL?
var name: String
}
func getFilters() -> [MyFilter] {
var filterArray = [MyFilter]()
if let filters = QuartzFilterManager.filters(inDomains: nil) {
for eachFilter in filters {
let filter = MyFilter(myURL: (eachFilter as! QuartzFilter).url(), name: (eachFilter as! QuartzFilter).localizedName()!)
filterArray.append(filter)
}
}
return filterArray
}
It's still not ideal. Having to cast eachFilter using as! QuartzFilter is clunky.
And other uses of ! are bad. Force-unwrapping the call to localizedName() can crash. Consider proper solutions.
Append is a method of the array struct, not of the URL/String.
You first need to create the array (you just declared it, you actually need to assign something to it)
You then need to create the object that you want to append into the array
You can now append this newly created object to the array
It should look something like this:
import Cocoa
import Quartz
class MyFilter {
var myURL: URL?
var name: String?
init(url: URL?, name: String?) {
self.myURL = url
self.name = name
}
}
func getFilters() -> Array<MyFilter> {
var filterArray = [MyFilter]()
if let filters = QuartzFilterManager.filters(inDomains: nil) {
for filter in filters {
let aFilter = MyFilter(url: filter.url(), name: filter.localizedName())
filterArray.append(aFilter)
}
}
return filterArray
}
Now the array returned by this method will have N MyFilter objects.
You can access every object in the array the way you did before, with
let aFilter = filterArray[index]
And to get the property inside of that object:
let url = aFilter.myURL
let name = aFilter.name
PS: I changed some names to fit the swift conventions (classes are written in PascalCase and variables in camelCase)
PpS: be careful with ! in swift, if it's used on something that happens to be nil will crash the app. Read more about optionals here
PpPs: I was just a few minutes late :D

Accessing final array after fetching from Firebase in Swift

I'm trying to fetch relational data from firebase in swift 3 and storing it in an array. It does fetch everything the way I want it to but i can't access the final array to work with.
I have tried everything I found online but can't make it work properly.
There are 3 child nodes I'm fetching, so every time it fetches it appends it to the array.
The output is:
success
success
success
I just want it to print "success" once.
Here is my code:
// Here the child with the relations is loaded
func fetchFollowingSection1IDs() {
guard let userID = FIRAuth.auth()?.currentUser?.uid else { return }
let reference = FIRDatabase.database().reference().child("interests").child("relations").child("userAndSection1").child(userID)
reference.observe(.childAdded, with: { (snapshot) in
// It's supposed to fetch the details of Section1 according to the childs from the relations (they are the IDs of Section1)
self.fetchSection1(section1ID: snapshot.key, completionHandler: { success in
guard success == true else {
return
}
print("success")
self.collectionView?.reloadData()
})
}, withCancel: nil)
}
// Here it gets the details from Firebase
func fetchSection1(section1ID: String, completionHandler: #escaping (Bool) -> ()) {
let ref = FIRDatabase.database().reference().child("interests").child("details").child("country").child("section1").child(section1ID)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
if let dictionary = snapshot.value as? [String: AnyObject] {
let section1 = Section1New(section1ID: section1ID, dictionary: dictionary)
self.section1s.append(section1)
}
completionHandler(true)
}) { (err) in
print("Failed to fetch section1s:", err)
}
}
My Firebase structure for the relations looks like this:
"interests" : {
"relations" : {
"userAndSection1" : {
"7fQvYMAO4yeVbb5gq1kEPTdR3XI3" : { // this is the user ID
"-KjS8r7Pbf6V2f0D1V9r" : true, // these are the IDs for Section1
"-KjS8tQdJbnZ7cXsNPm3" : true,
"-KjS8unhAoqOcfJB2IXh" : true
},
}
Everything loads properly and populates my collection views. It is just the problem that it is the wrong number of Section1s because of the triple appending to the array.
Thank you for your answers!
The code is doing exactly what you are telling it to do.
Your firebase event is .childAdded so it will iterate over each child node one at a time.
It first loads -KjS8r7Pbf6V2f0D1V9r and adds it to the section1s array - there is then one item in the array.
Then it loads -KjS8tQdJbnZ7cXsNPm3 and appends to the array. There's two items in the array and two lines output. etc.
The only thing we don't see in the code in your question is the line that actually prints the array, which is probably in your collectionView delegate methods.
Depending on your use case, you may want to read everything in with .value, and then iterate over that to populate your dataSource array.

Resources