SwiftUI Firebase Pagination Order Users by id - firebase

I have a page for users in a grid view and would like to paginate the view to reduce bandwidth when a user scrolls. For now i know how to paginate a feed view but cannot for a user grid view. Below is my code and what i am trying to achieve.
This is the code i use to paginate a feed view:
func fetchUserPosts() {
let query = Firestore.firestore().collection("posts").limit(to: 15).order(by: "timestamp", descending: true)
if let last = lastDoc {
let next = query.start(afterDocument: last)
next.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents, !documents.isEmpty else { return }
self.lastDoc = snapshot?.documents.last
self.posts.append(contentsOf: documents.compactMap({ try? $0.data(as: Post.self) }))
}
} else {
query.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents else { return }
self.posts = documents.compactMap({ try? $0.data(as: Post.self) })
self.lastDoc = snapshot?.documents.last
}
}
}
I am trying to achieve the same for a usergrid view, below is how far i have gone with errors i have encountered. Any help is appreciated.
class PeopleViewModel: ObservableObject {
#Published var users = [User]()
func fetchAllUsers() {
let query = Firestore.firestore().collection("users").limit(to: 20).order(by: "id", descending: true)
if let last = lastDoc {
let next = query.start(afterDocument: last)
next.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents, !documents.isEmpty else { return }
self.lastDoc = snapshot?.documents.last
self.users //Property is accessed but result is unused
}
} else {
query.getDocuments { snapshot, _ in
guard let documents = snapshot?.documents else { return }
self.users //Property is accessed but result is unused
self.lastDoc = snapshot?.documents.last
}
}
}
}

Related

Swiftui Force Update View

In my content view I have a home page with some text that says "Welcome, xxxx" where xxxx is the name fetched from a firebase database. This field can be changed in the settings page that is navigated to via a Navigation Link. When the name is changed and saved the name on the home page only updates when you force shutdown the app. How do I force update the view when you press the back button from settings.
This is how I display the field:
Text("Welcome, \(companyName)")
.font(.system(size: 23))
.bold()
.foregroundColor(Color("background"))
.padding(.bottom, 50)
This is how I set a value to companyName:
func SetData() {
var db = Firestore.firestore()
let user = Auth.auth().currentUser
let userName = user?.email ?? ""
let docRef = db.collection("CONTACT").document(userName)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//Setting Values
let data = document.data()
self.companyName = data?["companyName"] as? String ?? ""
} else {
print("Document does not exist")
}
}
}
There are several solutions to this, but you haven't provided enough code outlining what you have done to modify the variable companyName. The easiest solution would be to pass companyName as a binding value into the settings.
What I imagine here is that your HomeView is fetching the data on launch. In the settings, a change data request is made, but nothing is done to update the data in the HomeView. By using a binding variable we can ensure that the companyName connects to the source of truth in the HomeView, and so the function modifies the companyName which is precisely the company name on the HomeView vs. modifying potentially the value of companyName.
struct HomeView: View {
#State var companyName = "Microsoft"
var body: some View {
NavigationView {
NavigationLink(destination: SettingsView(companyName: $companyName)) {
Text("Tap to navigate to Settings")
}
}
}
}
struct SettingsView: View {
#Binding var companyName : String
var body: some View {
Button {
SetData()
} label: {
HStack {
Text("Tap to change!")
Text("\(companyName)!")
}
}
}
func SetData() {
var db = Firestore.firestore()
let user = Auth.auth().currentUser
let userName = user?.email ?? ""
let docRef = db.collection("CONTACT").document(userName)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//Setting Values
let data = document.data()
self.companyName = data?["companyName"] as? String ?? ""
} else {
print("Document does not exist")
}
}
}
}
If you have already done this at it doesn't somehow work, another solution is to add an .onAppear modifier to your HomeView.
struct HomeView: View {
#State var companyName = "Microsoft"
var body: some View {
VStack {
// code ...
}
.onAppear {
fetchData()
}
}
func fetchData() {
// code that returns companyFetchedName
self.companyName = companyFetchedName
}
}
Modify it on main queue, like
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//Setting Values
let data = document.data()
DispatchQueue.main.async { // << here !!
self.companyName = data?["companyName"] as? String ?? ""
}
} else {
print("Document does not exist")
}
}

How do I load data using ObservableObject in SwiftUI?

I am trying to transition an app from UIKit to SwiftUI which depends on basic DynamoDB resources but I have hit a snag in forcing the view to refresh as data is added to the list. I have been at this set of code for hours trying different things and I thought I might see if anyone might know why the 'SessionsData' seems to be thrown away and will not accumulate the 'Sessions' objects.
Does anyone have any quick thoughts???
class SessionsData: ObservableObject {
let didChange = PassthroughSubject<SessionsData, Never>()
#Published var data: [Sessions] = [] {
didSet {
didChange.send(self)
}
}
init() {
load()
}
func load() {
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 20
var temp : [Sessions] = []
dynamoDBObjectMapper.scan(Sessions.self, expression: scanExpression).continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
} else if let paginatedOutput = task.result {
for session in paginatedOutput.items as! [Sessions] {
print("Item Found")
temp.append(session)
}
DispatchQueue.main.async {
self.data = temp
self.didChange.send(self)
}
}
print(self.data.count)
return true
})
}
}
struct Events: View {
#ObservedObject var sessionsData = SessionsData()
var body: some View {...}
}
Looks like you've over-complicated the code. The PassthroughSubject is unnecessary. Whenever you change a #Published property, it should trigger an update.
class SessionsData: ObservableObject {
#Published var data: [Sessions] = []
init() {
load()
}
func load() {
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 20
var temp : [Sessions] = []
dynamoDBObjectMapper.scan(Sessions.self, expression: scanExpression).continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
} else if let paginatedOutput = task.result {
for session in paginatedOutput.items as! [Sessions] {
print("Item Found")
temp.append(session)
}
DispatchQueue.main.async {
self.data = temp
}
}
print(self.data.count)
return true
})
}
}
I don't have experience with DynamoDB, but here are a few things from SwiftUI / Combine perspective. In ObseravbleObjects have change a significant bit and and are now declared with objectWillChange and then sending newValue in willSet:
class SessionsData: ObservableObject {
public let objectWillChange = PassthroughSubject<[Sessions], Never>()
public private(set) var items: [Sessions] = [] {
willSet {
objectWillChange.send(newValue)
}
}
init() {
self.items = []
}
public func load() {
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 20
var temp: [Sessions] = []
dynamoDBObjectMapper
.scan(Sessions.self,
expression: scanExpression)
.continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
} else if let paginatedOutput = task.result,
let sessions = paginatedOutput.items as? [Sessions] {
temp.append(contentsOf: sessions)
}
DispatchQueue.main.async {
self.items = temp
}
}
return true
})
}
}
For the UI part you have to just call your load() method defined above in .onApear() and everything else should happen magically:
struct Events: View {
#ObservedObject var sessionsData: SessionsData
var body: some View {
List {
ForEach(self.sessionsData.items) { session in
Text(session.name) // or something of that kind
}
} .onAppear(perform: { self.sessionsData.load() })
}
}

accessing nested firebase data in swift

I am working wth a data structure, and I am looping through a couple nodes and here is the json data I get.
Snap (20171012) {
"-KwM45HyW4UduQgKTGn6" = {
ImageName = "Screen Shot 2017-10-13 at 11.24.51 AM.png";
fileURL = "";
thumbFileUrl = "";
user = "User not defined";
};
"-KwM4limD2aRyHgeKE5P" = {
ImageName = "test.png";
fileURL = "";
thumbFileUrl = "";
user = "User not defined";
};
}
After this, I can access the "snap" value using my data.key to get the "20171012"
ref.child(myselected_spot!).observe(DataEventType.value, with: { (snapshot) in
if snapshot.childrenCount > 0 {
for mydata in snapshot.children.allObjects as! [DataSnapshot]
{
if mydata.key.characters.count == 8 {
self.formattedDates.append(convertDate(stringDate: mydata.key))
self.selected_dates.append(mydata.key)
How would I get the value for "ImageName"
Your mydata is another DataSnapshot, so you can access all methods and properties of that class. In this case you're looking for DataSnapshot.childSnapshotForPath::
ref.child(myselected_spot!).observe(DataEventType.value, with: { (snapshot) in if snapshot.childrenCount > 0 {
for mydata in snapshot.children.allObjects as! [DataSnapshot]
{
if mydata.key.characters.count == 8 {
self.formattedDates.append(convertDate(stringDate: mydata.key))
self.selected_dates.append(mydata.key)
print(mydata.childSnapshot(forPath: "ImageName").value)
Pretty simple - I do not know what the variable myselected_Spot is but I am going to assume it's -KwM45HyW4UduQgKTGn6. If the below code does not yield results - I will need to know what that variable is.
ref.child(myselectd_spot).observe(.value, with: { (snapshot) in
if snapshot.value is NSNull{
//handles errors
return
}
else{
if let selectedSnapDict = snapshot.value as? NSDictionary {//Can also be [String: Any]
print(selectedSnapDict["ImageName"] as! String) //We know it's a string
}
else{
//null
}
}
})

Firebase deleted data still there

i have a behavior that I can't understand.
I delete a node on firebase database and I still receive the data during observing .value. But in firebase database the node is deleted.
I have a node called users_shoppinglists. Here are all id's of the users nodes to observe stored. Then I iterate all the id's to observe and call a function that observes each ID.
When I need to delete a list I update a node called status on the shoppinglists node and delete all to this list related data via cloud functions.
But the data is still received during observe. It seems I receive the data again before it is completely deleted.
Iterate all id's:
func ObserveAllList() -> Void{
if currentUser == nil { return }
self.ShowActivityIndicator()
ref.child("users_shoppinglists").child(currentUser!.id!).observe(.value, with: { (usersListsSnap) in
if usersListsSnap.value is NSNull { self.HideActivityIndicator(); return }
for listSnap in usersListsSnap.children {
let list = listSnap as! DataSnapshot
self.ObserveSingleList(listID: list.key)
}
}) { (error) in
NSLog(error.localizedDescription)
let title = String.OnlineFetchRequestError
let message = error.localizedDescription
self.ShowAlertMessage(title: title, message: message)
return
}
}
Call function to observe each ID:
func ObserveSingleList(listID:String) -> Void {
self.ShowActivityIndicator()
ref.child("shoppinglists").child(listID).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value is NSNull { self.HideActivityIndicator(); return }
//Read listData
var newList = ShoppingList()
newList.id = snapshot.key
newList.name = snapshot.childSnapshot(forPath: "listName").value as? String
newList.owneruid = snapshot.childSnapshot(forPath: "owneruid").value as? String
newList.relatedStore = snapshot.childSnapshot(forPath: "relatedStore").value as? String
//Read List items
self.ref.child("listItems").child(listID).observe(.value, with: { (itemSnap) in
var newItems = [ShoppingListItem]()
for items in itemSnap.children {
let item = items as! DataSnapshot
var newItem = ShoppingListItem()
newItem.id = item.key
newItem.listID = listID
newItem.isSelected = item.childSnapshot(forPath: "isSelected").value as? Bool
newItem.itemName = item.childSnapshot(forPath: "itemName").value as? String
newItem.sortNumber = item.childSnapshot(forPath: "sortNumber").value as? Int
newItems.append(newItem)
}
newList.items = newItems
//Read List members
self.ref.child("shoppinglist_member").child(listID).observe(.value, with: { (memberSnap) in
var newMembers = [ShoppingListMember]()
for members in memberSnap.children {
let member = members as! DataSnapshot
var m = ShoppingListMember()
m.memberID = member.key
m.status = member.value as? String
newMembers.append(m)
}
newList.members = newMembers
DispatchQueue.main.async {
if let index = allShoppingLists.index(where: { $0.id == listID }){
allShoppingLists[index] = newList
} else {
allShoppingLists.append(newList)
}
self.HideActivityIndicator()
NotificationCenter.default.post(name: Notification.Name.ShoppingBuddyListDataReceived, object: nil, userInfo: nil)
}
}, withCancel: { (error) in
self.HideActivityIndicator()
NSLog(error.localizedDescription)
let title = String.OnlineFetchRequestError
let message = error.localizedDescription
self.ShowAlertMessage(title: title, message: message)
return
})
}, withCancel: { (error) in
self.HideActivityIndicator()
NSLog(error.localizedDescription)
let title = String.OnlineFetchRequestError
let message = error.localizedDescription
self.ShowAlertMessage(title: title, message: message)
return
})
}) { (error) in
self.HideActivityIndicator()
NSLog(error.localizedDescription)
let title = String.OnlineFetchRequestError
let message = error.localizedDescription
self.ShowAlertMessage(title: title, message: message)
return
}
}
Cloud function:
//****************************************************************************************************************/
// Handles an action when status value changed in users_shoppinglists node
//****************************************************************************************************************/
exports.handle_ListStatusUpdate = functions.database.ref('/shoppinglists/{listID}').onUpdate(event => {
var listData = event.data.val()
console.log('Status', listData.status)
//handle deleted by owner
if (String(listData.status) == 'deleted by owner') {
//Get all members to delete the list on their users_shoppinglists node
return admin.database().ref('shoppinglist_member').child(event.params.listID).once('value').then(listMember => {
var promises = []
listMember.forEach(function (member) {
promises.push(admin.database().ref('users_shoppinglists').child(member.key).child(event.params.listID).set(null).then(() => {
return admin.database().ref('shoppinglist_member').child(event.params.listID).set(null).then(() => {
// delete the original shopping list
return admin.database().ref('shoppinglists').child(event.params.listID).set(null).then(() => {
return admin.database().ref('listItems').child(event.params.listID).set(null).then(() => {
})
})
})
}))
})
})
}
});/*********************************************************************************************************** */
Had this issue on Simulator. It was not only .value but .childRemoved and .childChanged were not triggered at all (only .childAdded was working).
Tried on iPhone and it worked. Then I made "Erase All Content And Settings..." to Simulator and it started to work again on Simulator too.
My bet is that firebase cache gets dirty during development, while you add or remove observers in code and probably change structure in database and at some point it just stops reacting appropriately.

Unable to make Graph + iCloud works

what I have to write here?
db = Graph(cloud: "iCloud.com.devname.appname", completion: { (done, error) in
if let errore = error {
debugPrint("Error iCloud: \(errore.localizedDescription)")
return
}
})
or
db = Graph(cloud: "fantasyString", completion: { (done, error) in
if let errore = error {
debugPrint("Errore iCloud: \(errore.localizedDescription)")
return
}
})
I tried everything but I'm unable to make iCloud works
Thank you for your help, Daniel
EDIT:
the way I read data form db:
var customers : [Entity] {
let search = Search<Entity>(graph: db).for(types: "Customers")
return search.sync(completion: nil).sorted { ($0["name"] as! String) < ($1["name"] as! String)}
}
the way I save the record:
func newCustomer(name:String, phone:String, mail:String, closure: #escaping ()->()) {
let cliente = Entity(type: "Customers")
cliente["name"] = name
cliente["phone"] = phone
cliente["mail"] = mail
db.sync { (done, error) in
if let errore = error {
debugPrint("Errore addCustomer: \(errore.localizedDescription)")
return
}
if done { closure() }
}
}
EDIT 2: the GraphDelegate implementation:
extension DataManager: GraphDelegate {
func graphWillPrepareCloudStorage(graph: Graph, transition: GraphCloudStorageTransition) {
debugPrint("graphWillPrepareCloudStorage")
if transition == .initialImportCompleted {
debugPrint("iCloud initialImportCompleted ok")
self.clientiCont?.tableView.reloadData()
}
}
func graphDidPrepareCloudStorage(graph: Graph) {
debugPrint("graphDidPrepareCloudStorage")
self.clientiCont?.tableView.reloadData()
}
func graphWillUpdateFromCloudStorage(graph: Graph) {
debugPrint("graphWillUpdateFromCloudStorage")
self.clientiCont?.tableView.reloadData()
}
func graphDidUpdateFromCloudStorage(graph: Graph) {
debugPrint("graphDidUpdateFromCloudStorage")
// refresh clienti
self.clientiCont?.tableView.reloadData()
// refresh lista ordini
self.gestCliCont?.tableOrder.reloadData()
// refresh oridine
self.gestOrdCont?.showOrder()
self.gestOrdCont?.tableProdotti.reloadData()
}
}
EDIT: the iCloud config
Thanks to one of my students I found the bug:
if you make a record this way everything works fine:
let record = Entity(type: "Names", graph: self.db)
but if you use this init it doesn't: let record = Entity(type: "Names")
so the solution is: make a record this way
let record = Entity(type: "Names", graph: self.db)

Resources