Working with Arrays and Firebase - swift

Looking to submit an array to firebase as a list of objects with integers as key names. I know that firebase does not support Arrays directly so was wondering how to do so. I am creating a list of items users add a users cart. so I am approaching it as such:
func addItemtoCart(item: String, completed: #escaping (_ completed: Bool) -> ()) { Firebase_REference_Cart.child(userID).child("itemIDs").updateChildValues(["itemiD": itemID])
completed(true)
}
I understand that this will not work because every time and item is added to the cart it will replace the item in under the "ItemId". I was looking to have something like this
CartITems: {
0: "945495949856956",
1: "9459469486895695"
2: "J888568567857685"
}
If someone could please describe how to do this from A to Z in the most descriptive way possible it would help greatly. I am new to firebase and need a bit of guidance.

First of all create model like:
struct Model {
var id: String
init(id: String) {
self.id = id
}}
Then make an instance of this model in your class: var model = Model(id: "First Id")
Then to push it to Firebase use:
func addInstance(model: Model) -> Completable {
return Completable.create( subscribe: { completable in
let reference = Database.database()
.reference()
.child("Models")
.childByAutoId
var model = model
model.id = reference.key
reference.setValue(model.dictionary)
self.modelVariable.value.append(model)
completable(.completed)
}
else {
completable(.error(NSError(domain: "Sample", code: 403, userInfo: [NSLocalizedDescriptionKey: "Server Error"])))
}
return Disposables.create()
})

Related

Reading Firestore Document containing an array of references

Thanks in advance for the help. I'm teaching myself Swift and trying to figure out how to retrieve the following data from Firebase. Here's my Firebase Data Model...
Groups (Collection)
-> GroupName (String)
-> Owner (References to someone in the Players collection)
Players (Collection)
-> PlayerFirstName
-> PlayerLastName
The Swift I've written to retrieve this data is in a ViewModel. getAllGroups is called from onAppear in the View and looks like this...
class Group: Identifiable, ObservableObject {
var id: String = UUID().uuidString
var name: String?
var owner: Player?
}
class GroupViewModel: ObservableObject {
#Published var groups = [Group]()
private var db = Firestore.firestore()
func getAllGroups() {
db.collection("groups").addSnapshotListener { (querySnapshot, error) in
guard let documents = querySnapshot?.documents else {
print("No groups")
return
}
self.groups = documents.map { (queryDocumentSnapshot) -> Group in
var group = Group()
let data = queryDocumentSnapshot.data()
group.name = data["name"] as? String ?? ""
//
// LIKE --- SHOULD THIS CALL TO GETPLAYER use AWAIT, FOR EXAMPLE?
// WE'RE EXECUTING THE CLOSURE FOR THE FIRST CALL AND ABOUT TO MAKE A SECOND
//
group.owner = self.getPlayer(playerRef: data["owner"] as! DocumentReference)
return group
}
}
}
func getPlayer(playerRef: DocumentReference) -> Player {
var player = Player()
playerRef.getDocument { (document, error) in
guard error == nil else {
print ("error", error ?? "")
return
}
if let document = document, document.exists {
let data = document.data()
if let data = data {
player.firstName = data["firstname"] as? String
player.lastName = data["lastname"] as? String
}
}
}
return player
}
}
The sorta obvious problem here is the closure for retrieving the parent Group executes and then goes and tries to retrieve the Owner. But by the time the closure inside getPlayer completes... the Group has already been established.
Groups will have...
group[0]
-> GroupName = "Cool Name Here"
-> Owner = nil
group[0]
-> GroupName = "Different Cool Name"
-> Owner = nil
even though each Group definitely has an Owner.
I get there's some stuff here about asynchronous calls in Swift and how best to handle that... I'm just not sure what the proper pattern is. Thanks again for the help and advice!
-j
To restate the question:
How do you nest Firestore functions
There are 100 ways to do it and, a lot of it depends on the use case. Some people like DispatchGroups, others like escaping completion handlers but in a nutshell, they pretty much do the "same thing" as the following code, written out "long hand" for readability
func populateGroupArray() {
db.collection("groups").addSnapshotListener { (querySnapshot, error) in
guard let docs = querySnapshot?.documents else { return }
for doc in docs {
let groupName = doc.get("name") as! String
let ownerId = doc.get("owner_id") as! String
self.addToArray(groupName: groupName, andOwnerId: ownerId)
}
}
}
func addToArray(groupName: String, andOwnerId: String) {
db.collection("owners").document(andOwnerId).getDocument(completion: { snapshot, error in
let name = snapshot?.get("owner_name") as! String
let group = Group(groupName: groupName, ownerName: name)
self.groups.append(group)
})
}
In summary; calling populateGroupArray reads in all of the documents from the groups collection from Firestore (adding a listener too). We then iterate over the returned documents to get each group name and the owner id of the group.
Within that iteration, the group name and ownerId are passed to another function that reads in that specific owner via it's ownerId and retrieves the name
Finally, a Group object is instantiated with groupName and owner name being populated. That group is then added to a class var groups array.
Now, if you ask a Firebaser about this method, they will generally recommend not reading large amounts of Firebase data 'in a tight loop'. That being said, this will work very well for many use cases.
In the case you've got a HUGE dataset, you may want to consider denormalizing your data by including the owner name in the group. But again, that would be a rare situation.

How to merge two queries using Firestore - Swift

I need to merge two queries with firebase firestore and then order the results using the timestamp field of the documents.
Online I didn't find much information regarding Swift and Firestore.
This is what I did so far:
db.collection("Notes").whereField("fromUid", isEqualTo: currentUserUid as Any).whereField("toUid", isEqualTo: chatUserUid as Any).getDocuments { (snapshot, error) in
if let error = error {
print(error.localizedDescription)
return
}
db.collection("Notes").whereField("fromUid", isEqualTo: self.chatUserUid as Any).whereField("toUid", isEqualTo: self.currentUserUid as Any).getDocuments { (snaphot1, error1) in
if let err = error1{
print(err.localizedDescription)
return
}
}
}
I added the second query inside the first one on completion but now I don't know how to merge them and order them through the field of timestamp.
On this insightful question It is explained that it's recommended to use a Task object but I don't find anything similar with swift.
There are many ways to accomplish this; here's one option.
To provide an answer, we have to make a couple of additions; first, we need somewhere to store the data retrieved from firebase so here's a class to contains some chat information
class ChatClass {
var from = ""
var to = ""
var msg = ""
var timestamp = 0
convenience init(withDoc: DocumentSnapshot) {
self.init()
self.from = withDoc.get("from") as! String
self.to = withDoc.get("to") as! String
self.msg = withDoc.get("msg") as! String
self.timestamp = withDoc.get("timestamp") as! Int
}
}
then we need a class level array to store it so we can use it later - perhaps as a tableView dataSource
class ViewController: NSViewController {
var sortedChatArray = [ChatClass]()
The setup is we have two users, Jay and Cindy and we want to retrieve all of the chats between them and sort by timestamp (just an Int in this case).
Here's the code that reads in all of the chats from one user to another creates ChatClass objects and adds them to an array. When complete that array is passed back to the calling completion handler for further processing.
func chatQuery(from: String, to: String, completion: #escaping( [ChatClass] ) -> Void) {
let chatsColl = self.db.collection("chats") //self.db points to my Firestore
chatsColl.whereField("from", isEqualTo: from).whereField("to", isEqualTo: to).getDocuments(completion: { snapshot, error in
if let err = error {
print(err.localizedDescription)
return
}
guard let docs = snapshot?.documents else { return }
var chatArray = [ChatClass]()
for doc in docs {
let chat = ChatClass(withDoc: doc)
chatArray.append(chat)
}
completion(chatArray)
})
}
Then the tricky bit. The code calls the above code which returns an array The above code is called again, returning another array. The arrays are combined, sorted and printed to console.
func buildChatArray() {
self.chatQuery(from: "Jay", to: "Cindy", completion: { jayCindyArray in
self.chatQuery(from: "Cindy", to: "Jay", completion: { cindyJayArray in
let unsortedArray = jayCindyArray + cindyJayArray
self.sortedChatArray = unsortedArray.sorted(by: { $0.timestamp < $1.timestamp })
for chat in self.sortedChatArray {
print(chat.timestamp, chat.from, chat.to, chat.msg)
}
})
})
}
and the output
ts: 2 from: Cindy to: Jay msg: Hey Jay, Sup.
ts: 3 from: Jay to: Cindy msg: Hi Cindy. Not much
ts: 9 from: Jay to: Cindy msg: Talk to you later

Vapor 3 - How to populate array property of model from Leaf template form

I have a Group, User and an App model. Within my Group model, I have a property var apps: [App] and I create a sibling relationship bw Group and User.
In my WebsiteController, I have 2 handlers:
createGroupHandler which handles the GET request at /groups/create
createGroupPostHandler which handles the POST request at /groups/create
My problem is that after selecting app objects in my form in my createGroup.leaf template, when I create my new group no apps are being populated into my apps array.
I created structs to represent the context being rendered in my createGroupHandler and to handle the data being passed in the post request for createGroupPostHandler
struct CreateGroupContext: Encodable {
let title = "Create Group"
let users: Future<[User]>
let apps: Future<[App]>
}
struct CreateGroupData: Content {
let name: String
let apps: [App]
let users: [String]?
}
The apps are properly loaded into the form but despite selecting them like in the image below, they aren't added to the array
My createGroupPostHandler looks like below. I don't know how to grab the selected apps and populate my apps: [App] array when I create my group is my issue, I feel like this is where I should be doing that, but I don't know how to grab that from the Leaf template.
func createGroupPostHandler(_ req: Request, data: CreateGroupData) throws -> Future<Response> {
let group = Group(name: data.name, apps: data.apps)
return group.save(on: req).flatMap(to: Response.self) { group in
guard let id = group.id else {
throw Abort(.internalServerError)
}
var userSaves: [Future<Void>] = []
for user in data.users ?? [] {
try userSaves.append(User.addUser(user, to: group, on: req))
}
let redirect = req.redirect(to: "/groups/\(id)")
return userSaves.flatten(on: req).transform(to: redirect)
}
}
This is what my createGroup.leaf looks like:
I use index notation of naming input tags in HTML, such as score[0], score[1], etc. Vapor is then quite happy to parse this and create a dictionary (okay, technically not an array!).
Use this notation in the Leaf file:
#for(result in results) {
<input type="text" name="score[#(index)]" value="#(result.score)">
}
The controller function:
struct SaveResult:Content
{
var score:[Int:String]
}
func saveRoll(_ request:Request) throws -> Future<View>
{
return try request.content.decode(SaveResult.self).flatMap
{
result in
for i in 0...(result.score.count) - 1
{
print( detail.score[i] )
}
return try self.index(request)
}
}

Inserting data into realm DB with progress?

I have request data which was about 7MB after it has downloaded the json string,means the json string is about 7MB.After it has downloaded,I would like to save the data into realm model object(table) with progress like
(1/7390) to (7390/7390) -> (data which is inserted/total data to be inserted)
I am using Alamofire as HTTPClient at my app.So,how to insert data with progress into my realm object model after it has downloaded from server?Any help cause I am a beginner.
I wont show the data model exactly,so,any example is appreciated.Let say my json string is.
{
{
name : Smith,
age : 23,
address : New York
},
{
name : Adam,
age : 22,
address : Maimi
},
{
name : Johnny,
age : 33,
address : Las Vegas
},
... about 7392 records
}
Supposing you have a label for do this.
Ok.
Supposing you using MVVM pattern too.
Ok.
ViewController has label and "observe"(*) the ViewModel property 'progress'
ViewModel has property 'progress'
class ViewModel: NSObject {
dynamic var progress: Int = 0
func save(object object: Object) {
do {
let realm = try Realm()
try realm.write({ () -> Void in
// Here your operations on DB
self.progress += 1
})
} catch let error as NSError {
ERLog(what: error)
}
}
}
In this way viewController is notified when "progress" change and you can update UI.
Your VC should have a method like this, called by viewDidLoad for instance:
private func setupObservers() {
RACObserve(self.viewModel, keyPath: "progress").subscribeNext { (next: AnyObject!) -> Void in
if let newProgress = next as? Int {
// Here update label
}
}
}
Where RACObserve is a global function:
import Foundation
import ReactiveCocoa
func RACObserve(target: NSObject!, keyPath: String) -> RACSignal {
return target.rac_valuesForKeyPath(keyPath, observer: target)
}
(*) You can use ReactiveCocoa for instance.
Katsumi from Realm here. First, Realm has no way to know the total amount of data. So calculating progress and notifying it should be done in your code.
A total is a count of results array. Store count as a local variable. Then define progress variable to store the number of persisted. progress should be incremented every save an object to the Realm. Then notify the progress.
let count = results.count // Store count of results
if count > 0{
if let users = results.array {
let progress = 0 // Number of persisted
let realm = try! Realm()
try realm.write {
for user in users{
let userList=UserList()
[...]
realm.add(userList,update: true)
progress += 1 // After save, increment progress
notify(progress, total: count)
}
}
}
}
So there are several ways to notify the progress. Here we use NSNotificationCenter for example:
func notify(progress: Int, total: Int) {
NSNotificationCenter
.defaultCenter()
.postNotificationName("ProgressNotification", object: self, userInfo: ["progress": progress, "total": total])
}

RxSwift Using Variables Correctly

I am trying to convert a project to use RxSwift and MVVM. I have a service that syncs a list of data from Parse on every app launch and I basically want to make sure I am taking the correct approach.
What I have done is made a Variable subject and then allow my models to listen to this.
ParseService:
let rx_parseMushrooms = Variable<[ParseMushroom]>([])
MushroomLibraryModel:
_ = parseService.rx_parseMushrooms
.asObservable()
.map { (parseMushrooms:[ParseMushroom]) -> [Mushroom] in
let mushrooms = parseMushrooms.map { (parseMushroom:ParseMushroom) -> Mushroom in
let mushroom = Mapper<Mushroom>().map(parseMushroom.dictionaryWithValuesForKeys(parseMushroom.allKeys()))
return mushroom!
}
return mushrooms
}
.subscribeNext({ (mushrooms:[Mushroom]) -> Void in
self.mushrooms = mushrooms
print(mushrooms)
})
I do the same for expressing the sync state.
ParseService:
struct SyncState {
enum State {
case Unsynced, ConnectingToServer, SyncingInfo, FetchingImageList, SyncingImages, SyncComplete, SyncCompleteWithError
}
var infoToSync = 0
var imagesToSync = 0
var imagesSynced = 0
var state = State.Unsynced
}
let rx_syncState = Variable(SyncState())
I then update the variable a la
self.rx_syncState.value = self.syncState
SyncViewModel:
_ = parseService.rx_syncState
.asObservable()
.subscribeNext { [weak self] (syncState:ParseService.SyncState) -> Void in
switch syncState.state {
//show stuff based on state struct
}
}
Anyways, I would greatly appreciate if someone can tell me if this is a good way of going about it or if I am misusing RxSwift (and guide me on how I should be doing this).
Cheers!
Hmm... Here is an article about using Variable (note that Variable is a wrapper around BehaviorSubject.)
http://davesexton.com/blog/post/To-Use-Subject-Or-Not-To-Use-Subject.aspx
In your case, you already have a cold observable (the network call,) so you don't need a Subject/Variable. All you need to do is publish the observable you already have and use replay(1) to cache the value. I would expect a class named something like ParseServer that contains a computed property named something like mushrooms.
To help get the mushrooms out of parse, you could use this (this will create the cold observable you need):
extension PFQuery {
var rx_findObjects: Observable<[PFObject]> {
return Observable.create { observer in
self.findObjectsInBackgroundWithBlock({ results, error in
if let results = results {
observer.on(.Next(results))
observer.on(.Completed)
}
else {
observer.on(.Error(error ?? RxError.Unknown))
}
})
return AnonymousDisposable({ self.cancel() })
}
}
}
And then you would have something like:
class ParseServer {
var mushrooms: Observable<[Mushroom]> {
return PFQuery(className: "Mushroom").rx_findObjects
.map { $0.map { Mushroom(pfObject: $0) } }
.publish()
.replay(1)
}
}
I think the above is correct. I didn't run it through a compiler though, much less test it. It might need editing.
The idea though is that the first time you call myParseServer.mushrooms the system will call Parse to get the mushrooms out and cache them. From then on, it will just return the previous cashed mushrooms.