Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
how do I stop or bypass the auto reload in Firebase Realtime Database, I want to manually reload the data. for instance, I want to use UIRefresh controller instead of using firebase cache reload
If you want to only load data once, and not automatically get notified of changes, you can use the observeSingleEvent methods.
Also see the documentation on reading data once, which contains the following example:
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String ?? ""
let user = User(username: username)
// ...
}) { (error) in
print(error.localizedDescription)
}
The above example reads the data of users/$userID once, either from the server or from the local cache. A note on that last aspect: you shouldn't combine observeSingleEvent with enabling disk persistence as they don't work well together as explained in Firebase Offline Capabilities and addListenerForSingleValueEvent
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 19 hours ago.
Improve this question
I am trying to develop an app that shows a trip on a map from a list of trips.
I have a list of trips in a bottom sheet and when I click on one of these I would like to update the map showing the information of the trip (the tripPoints on the map).
struct Trip {
let id: String
let tripPoints: [TripPoints]
}
struct TripPoints {
let id: String
let latitude: CLLocationDegrees
let longitude: CLLocationDegrees
}
I tried to use a variable selectedTrip that I update everytime i click on one of the trip, but the map doesn't update.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
How can I make a check for the correctness of the entered data?
For example, the user needs to enter a digit, and until this is done, the program will not skip it further
In my case, you need to make sure that the user entered an integer, and not a string, double, etc.
Just create an infinite loop. If your read is successful break it. Something like:
while true {
if let line = readLine(), let value = Int(line) {
print("value", value)
break
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am working on an app in swift and need help. I am trying to have a search object on the screen and when you type letters it will bring a result from the realtime database using firebase. Please help. I don't know how I can code that. Please Please Please Help.
You have to be a bit more specific about what exactly is it that you are trying to do. Firstly - what technologies are you using? JavaScript? What framewroks? React? Angular? Vuejs? Vanilla JavaScript? Is it a restful application you are doing or server rendered?
Depending on your answers to the above questions the code can vary greatly. Here I can give you a general solution though:
first create the search box and a button with an action that sends a request (mostly HTML)
that request can be an AJAX GET request to look for results in the database, based on a certain search keyword. I recommend a library called axios to help you with that. (sending and resolving promises with JavaScript)
handle the response of that request and if the response is valid (with status 200) then update your search results. (mostly JavaScript)
I just did this a few weeks ago for my app you are in luck! I am using swift programming language btw.
So according to firebase you should not use firebase to search for documents in the database. Firebase Documentation on this.. Instead you should use something else like "Algolia", "Elastic", or "typeSense". I chose to use Algolia and it took me about a day to implementing my app. Super easy.
But if you really want to use firebase (which is not recommended) you could use this:
func loadPost(lastDoc: QueryDocumentSnapshot?, completion: #escaping (QueryDocumentSnapshot?) -> Void) {
let db: Query
// Check to see if previous documents have been loaded or not.
if let lastDoc = lastDoc {
db = Firestore.firestore().collection("collection name")
.order(by: "document name you are wanting to get. example: postTitle, username, date, etc")
// Amount of documents you want to retrieve from your colleciton
.limit(to: 10)
// Check certain conditions that you want to retrieve.
// In this example we are only retrieving the posts that have 100 reports or greater.
.whereField("reportCount", isGreaterThan: 100)
// Start after the previous loaded document so we don't waste data by reloading documents that have already been loaded.
.start(afterDocument: lastDoc)
// If there are no previous loaded documents then you just start from the beginning of your collection colleciton.
} else {
db = Firestore.firestore().collection("posts")
.order(by: "document name", descending: true)
.limit(to: 10)
.whereField("document name", isGreaterThan: 100)
}
// finally get the documents you want from firebase
db.getDocuments { query, error in
// Check for errors
if let query = query, error == nil {
// Not really sure why you need a dispatchgroup, but its kind of like a completionHandler for a for loop.
let group = DispatchGroup()
for doc in query.documents {
group.enter()
// run the code you need to run:
// After the code is finished running you need to call group.leave()
group.leave()
}
// After the for loop is completely done call group.notify which is basically like a completionHandler
group.notify(queue: .main) {
// notify whatever needs to be notified.
// finally insert the last document loaded so when we need to load data again it only loads documents that have not been loaded.
completion(query.documents.last)
}
} else {
completion(nil)
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
New programmer here! I'm working with a network request and below is a snippet of my code. After I complete the network request I want to store a value from the request into a variable. However, when the variable is called outside of the if let statement it is NOT updated. Essentially, the print statement inside the if let statement prints the value I'm looking for, but the print statement outside of the if let statement prints the default value I set for that variable. The variable is global. I need to use that updated variable elsewhere in my code, any ideas?
if let data = data {
self.nameLabel.text = data.name
person = data.name
print(person)
}
print(person)
First of all you didn't showed complete code so I am guessing you need main queue to update your UILabel first as shown in below code:
DispatchQueue.main.async {
self.nameLabel.text = data.name
}
next thing is if you want to access the value somewhere else once your request completes and UILabel updated then you can get the value from it this way:
let value = self.nameLabel.text!
And if you want to access the value once request complete then you can use closure and HERE you can check example of it.
I'm using swift core data to save user's info within the app but when its saving current users data (name,bio, 3-4 photos) it hangs for 2-3 seconds until it saves the data. What is the best way to avoid this hanging of the app? this is the code I'm using
for photo in photos {
let data = NSData(contentsOfURL: NSURL(string: photo.url!)!)
let newimg = UIImage(data: data!)
newusersImages.addObject(newimg!)
}
mayBeUser.name = currentuser!.objectForKey("name") as! String
let arrayData = NSKeyedArchiver.archivedDataWithRootObject(newusersImages)
let locationData = NSKeyedArchiver.archivedDataWithRootObject(currentuser!.objectForKey("location")!)
mayBeUser.photos = arrayData
mayBeUser.location = locationData
mayBeUser.about = currentuser!.objectForKey("about") as! String
mayBeUser.setValue(currentuser!.objectForKey("age") as! Int, forKey: "age")
mayBeUser.objectId = currentuser!.objectId!
mayBeUser.lastSeen = currentuser!.objectForKey("lastSeen") as! NSDate
try! context.save()
print("Current User Updated")
when the user has 10-15 friends , it takes hangs for a minute to save/update all the info/data of the users.
Here's how I'd do it. Now obviously you don't want the UI to hang, so you'll want to use a managed object context that has a private queue concurrency type. More info on that here. Specifically, look at the part at the end where he details how to create the export function.
Now, since you're saving data to the persistent store in this case, you might not want the user to go around changing stuff while their previous changes are being saved, in this case you might want an interim screen showing the progress of the save.
You (and your app) are having to work hard to save all that photo data into one attribute of your User entity. Instead, restructure your data model:
Create a separate Photo entity for the photos.
Don't store the photo image data in CoreData - just store a string with the URL.
Add a to-many relationship from User to Photo, instead of the photos attribute.
Incidentally, I would avoid using objectId as an attribute name: it's so close to CoreData's objectID it will cause confusion.