I have been trying to implement a "follow" function on my app. Essentially when a user hits the "follow" button, we run a runTransactionBlock to update integer values we are storing on Firebase database for the user and the account they are following. The issue is that I am able to update the counter for the user (say John in example below) , but I am not able to update the counter for the user I am following (say olivia in example below).
Currently the Firebase nodes look as such:
user_profiles{
UID1:{
name: john
following: 1 //code will update for my account
followers: 0
},
UID2:{
name: olivia
following: 0
followers: 0 //code will not update count for person i am trying to follow
I have referenced the following, however I am still facing issues with getting this to work. If anyone can please glance through and point me in the right direction, it would be greatly appreciated.
https://www.firebase.com/docs/ios/guide/saving-data.html
Firebase database help - Swift
Upvote/Downvote system within Swift via Firebase
var guestUIDToPass = String()
var loggedInUser = AnyObject()
#IBAction func didTapFollow(sender: AnyObject) {
following()
}
func following() {
self.loggedInUser = FIRAuth.auth()?.currentUser
//updating count for user, works perfectly
self.databaseRef.child("user_profiles").child(self.loggedInUser.uid).child("following").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Int
if (value == nil) {
value = 0
}
currentData.value = value! + 1
return FIRTransactionResult.successWithValue(currentData)
})
//updating count for person user is following, doesn't update firebase
self.databaseRef.child("user_profiles").child("\(self.guestUIDToPass)").child("followers").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Int
if (value == nil) {
value = 0
}
currentData.value = value! + 1
return FIRTransactionResult.successWithValue(currentData)
})
}
Try:-
let prntRef = FIRDatabase.database().reference().child("user_profiles").child(whomIFollowedUID).child("following")
prntRef.runTransactionBlock({ (following) -> FIRTransactionResult in
if let followNum = following.value as? Int{
following.value = followNum + 1
return FIRTransactionResult.successWithValue(following)
}else{
return FIRTransactionResult.successWithValue(following)
}
}, andCompletionBlock: {(error,completion,snap) in
print(error?.localizedDescription)
print(completion)
print(snap)
if !completion {
print("The value wasn't able to Update")
}else{
//Updated
}
})
Related
I've got this issue when running my code to update a few records. I have 138 records.
I've set limit to get 1000 record per one request and then I've tried to update my new column I've created:
But I get this error when I save PFObject in background
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.
I found this link with similar problem, but looks like that ticket is resolved and closed.
Looking in my code below I am trying to cycle over 138 records to set new key value for PFObject and then I save it. Normally I don't need such operation, maybe this happens because of lot of records were updated at once. But just wonder if this is still the case in api.
This is my code:
let salaryRepository = SalaryDataRepository(remoteDataSource: SalaryRemoteDataSource(), localDataSource: SalaryLocalDataSource())
salaryRepository.getSalaries(with: nil, payPeriod: nil, paidDateDoesNotExist: false, limit: 0, skip: 0, completion: { (result) in
switch result {
case let .success(salaries):
guard let unwrappedSalaries = salaries else {
return
}
var counter = 0
for salary in unwrappedSalaries {
var totalQuote1 = 0.0
if let pfSalary = salary.getPFSalary() {
if let subTotal = pfSalary["subTotal"] as? NSNumber, let unwrappedCustomExchangeRate = pfSalary["customExchangeRate"] as? NSNumber {
if let pfProjectEmployee = pfSalary["projectEmployee"] as? PFObject {
if let pfProjectEmployeeDetails = pfProjectEmployee["projectEmployeeDetails"] as? PFObject {
if let transactionFee = pfProjectEmployeeDetails["transactionFee"] as? NSNumber {
let subTotalQuote = NSNumber.getMultiplying(a: subTotal, b: unwrappedCustomExchangeRate)
let totalQuote = subTotalQuote.afterFee(fee: transactionFee)
pfSalary["totalQuote"] = totalQuote
totalQuote1 = totalQuote.doubleValue
print(transactionFee)
counter = counter + 1
}
}
}
pfSalary.saveInBackground { (success, error) in
if error == nil && success == true {
print("SUCCESS:")
print(totalQuote1)
} else {
print("ERROR:")
print(totalQuote1)
}
}
}
}
}
print("total updated:")
print(counter)
break
case let .failure(error):
print(error)
break
}
})
In a tableView I have a list of jobs. These jobs can be accessed by multiple users, therefore I need to use FIRTransaction. Based on the result of the first write to FirebaseDatabase, I need to write/not write to another path in Firebase.
The schema is as follows:
//Claim job by - Cleaner
//cleaner tries to claim booking at Users/UID/cus/bookingNumber
//if FIRMutableData contains a key ‘claimed’ or key:value “claimed”:true
//update at Users/UID/cus/bookingNumber with key:value
//based on response received write or not to another path
//write to Cleaners/UID/bookingNumber
//key:value
If the internet connection drops before client app receives response from firebase server, write to Cleaners/UID/bookingNumber will not be made.
How can I solve this problem?
#IBAction func claimJob(_ sender: Any) {
dbRef.runTransactionBlock({ (_ currentData:FIRMutableData) -> FIRTransactionResult in
//if valueRetrieved is nil abort
guard let val = currentData.value as? [String : AnyObject] else {
return FIRTransactionResult.abort()
}
self.valueRetrieved = val
guard let uid = FIRAuth.auth()?.currentUser?.uid else {
print("abort no uid line 80")
return FIRTransactionResult.abort()
}
self.uid = uid
for key in self.valueRetrieved.keys {
//unwrap value of 'claimed' key
guard let keyValue = self.valueRetrieved["Claimed"] as? String else {
print("abort line 88")
return FIRTransactionResult.abort()
}
//check if key value is true
if keyValue == "true"{
//booking already assigned show alert,stop transaction
self.alertText = "Booking already taken, please refresh table!"
self.alertActionTitle = "OK"
self.segueIdentifier = "unwindfromClaimDetailToClaim"
self.showAlert()
return FIRTransactionResult.abort()
} else {
//write the new values to firebase
let newData = self.createDictionary()
currentData.value = newData
return FIRTransactionResult.success(withValue: currentData)
}//end of else
}//end of for key in self
return FIRTransactionResult.abort()
}) {(error, committed,snapshot) in
if let error = error {
//display an alert with the error, ask user to try again
self.alertText = "Booking could not be claimed, please try again."
self.alertActionTitle = "OK"
self.segueIdentifier = "unwindfromClaimDetailToClaim"
self.showAlert()
//what if internet connection drops here or client quits app ????????????
} else if committed == true {
//write to Cleaners/UID/bookingNumber
//what if internet connection drops here or client quits app??????
self.cleanersRef.setValue(snapshot?.value)
self.alertText = "Booking claimed.Please check your calendar"
self.alertActionTitle = "OK"
self.segueIdentifier = "unwindfromClaimDetailToClaim"
self.showAlert()
}
}
}//end of claimJob button
I am wanting to update the child values after editing inside the textfields.
At the moment I have this action:
#IBAction func updateAction(_ sender: Any) {
guard let itemNameText = itemName.text, let itemDateText = itemDate.text else { return }
guard itemNameText.characters.count > 0, itemDateText.characters.count > 0 else {
print("Complete all fields")
return
}
let uid = FIRAuth.auth()?.currentUser?.uid
let key = item.ref!.key
let itemList = Item(itemName: itemNameText, itemDate: itemDateText, uid: uid!)
let editItemRef = databaseRef.child("/usersList/\(key)")
editItemRef.updateChildValues(itemList.toAnyObject())
print("edited")
}
I was following this tutorial but he seems to use the username, and as I only have the email or uid (userID) as authentication I thought I'd use the uid.
This is my toAnyObject function inside my class:
func toAnyObject() -> [String: AnyObject] {
return ["itemName": itemName as AnyObject, "itemDate": itemDate as AnyObject, "userID": userID as AnyObject]
}
When I run the breakpoint it does show the edited value of the item however the update doesn't appear to be performing.
Just to be extra safe, try dropping the leading slash from your path:
databaseRef.child("usersList/\(key)")
…and try printing the Error returned by Firebase, if any:
editItemRef.updateChildValues(itemList.toAnyObject()) {
(error, _) in
if let error = error {
print("ERROR: \(error)")
} else {
print("SUCCESS")
}
Edit. We found out he was using the wrong database path. The right one is:
databaseRef.child("users").child(uid!).child("usersList/\(key)")
How do you check to see if a value is greater than a Firebase value and only write over it if the new value is higher?
This is my current code when scores are submitted but it just replaces the score value with the new one whether or not it is greater and adds to the total score either way as well. I need to only update the score if it's higher and only add to the total score if it's higher.
let dbRef2 = FIRDatabase.database().reference()
dbRef2.queryOrdered(byChild: "Live Scoring Male/\(String(describing: CompLabel.text!))/score\(String(describing: ProblemLabel.text!))").observe(FIRDataEventType.value, with: { (snapshot) in
var previousScore = snapshot.value as? Double
print (previousScore)
})
if Double(ScoreLabel.text!)! > previousScore {
self.dbRef.child("Live Scoring Male/\(String(describing: CompLabel.text!))/score\(String(describing: ProblemLabel.text!))").setValue(Int(ScoreLabel.text!))
self.dbRef.child("Live Scoring Male/\(String(describing: CompLabel.text!))/attempts\(String(describing: ProblemLabel.text!))").setValue(Int(AttemptsLabel.text!))
let dbRef1 = FIRDatabase.database().reference().child("Live Scoring Male/\(String(describing: CompLabel.text!))/scoreTotal")
dbRef1.queryOrdered(byChild: "scoreTotal").observe(FIRDataEventType.value, with: { (snapshot) in
let currentScore = snapshot.value as? Double
let newScore = ["scoreTotal": Double(currentScore!) + Double(self.ScoreLabel.text!)! - Double(self.AttemptsLabel.text!)!*0.1]
self.dbRef.child("Live Scoring Male/\(String(describing: self.CompLabel.text!))").updateChildValues(newScore)
})
dbRef1.removeAllObservers()
}
In order to do this you'll need to query your database for the current score and then check client side if the new value is greater than the current value. I'm not sure what kind of app you have but if your app allows multiple users to post their new scores simultaneously then should do this using a transaction block just incase two people do it at the exact same time
Here is an example of how to use a transaction block for your situation...
func updateScore(newScore: Int){
let ref = fireRootRef.child("currentScore")
ref.runTransactionBlock({ (data)-> FIRTransactionResult in
print("ENETERED TRANSACTION BLOCK and data is \(data)")
//here `value` is equal to the current value stored in firebase
var value = data.value as? Int
if value == nil {
value = newScore
} else if value! < newScore {
value = newScore
}
return FIRTransactionResult.success(withValue: data)
},
andCompletionBlock: {(error, bool, snapshot)-> Void in
if error != nil{
print("ERROR in transaction block \(error)")
}
print("bool value in completion block = \(bool)")
print("transaction block snapshot = \(snapshot)")
})
}
If only one user can alter this value then you can simply query the database like
func updateScore(newScore: Int) {
let ref = fireRootRef.child("currentScore")
ref.observeSingleEvent(of: .value, with: {(snapshot)-> Void in
if let currentScore = snapshot.value as? Int, newScore > currentScore {
ref.setValue(newScore)
}
})
}
I have this Firebase data:
I want to query the posts data through pagination. Currently my code is converting this JS code to Swift code
let postsRef = self.rootDatabaseReference.child("development/posts")
postsRef.queryOrderedByChild("createdAt").queryStartingAtValue((page - 1) * count).queryLimitedToFirst(UInt(count)).observeSingleEventOfType(.Value, withBlock: { snapshot in
....
})
When accessing, this data page: 1, count: 1. I can get the data for "posts.a" but when I try to access page: 2, count: 1 the returns is still "posts.a"
What am I missing here?
Assuming that you are or will be using childByAutoId() when pushing data to Firebase, you can use queryOrderedByKey() to order your data chronologically. Doc here.
The unique key is based on a timestamp, so list items will automatically be ordered chronologically.
To start on a specific key, you will have to append your query with queryStartingAtValue(_:).
Sample usage:
var count = numberOfItemsPerPage
var query ref.queryOrderedByKey()
if startKey != nil {
query = query.queryStartingAtValue(startKey)
count += 1
}
query.queryLimitedToFirst(UInt(count)).observeSingleEventOfType(.Value, withBlock: { snapshot in
guard var children = snapshot.children.allObjects as? [FIRDataSnapshot] else {
// Handle error
return
}
if startKey != nil && !children.isEmpty {
children.removeFirst()
}
// Do something with children
})
I know I'm a bit late and there's a nice answer by timominous, but I'd like to share the way I've solved this. This is a full example, it isn't only about pagination. This example is in Swift 4 and I've used a nice library named CodableFirebase (you can find it here) to decode the Firebase snapshot values.
Besides those things, remember to use childByAutoId when creating a post and storing that key in postId(or your variable). So, we can use it later on.
Now, the model looks like so...
class FeedsModel: Decodable {
var postId: String!
var authorId: String! //The author of the post
var timestamp: Double = 0.0 //We'll use it sort the posts.
//And other properties like 'likesCount', 'postDescription'...
}
We're going to get the posts in the recent first fashion using this function
class func getFeedsWith(lastKey: String?, completion: #escaping ((Bool, [FeedsModel]?) -> Void)) {
let feedsReference = Database.database().reference().child("YOUR FEEDS' NODE")
let query = (lastKey != nil) ? feedsReference.queryOrderedByKey().queryLimited(toLast: "YOUR NUMBER OF FEEDS PER PAGE" + 1).queryEnding(atValue: lastKey): feedsReference.queryOrderedByKey().queryLimited(toLast: "YOUR NUMBER OF FEEDS PER PAGE")
//Last key would be nil initially(for the first page).
query.observeSingleEvent(of: .value) { (snapshot) in
guard snapshot.exists(), let value = snapshot.value else {
completion(false, nil)
return
}
do {
let model = try FirebaseDecoder().decode([String: FeedsModel].self, from: value)
//We get the feeds in ['childAddedByAutoId key': model] manner. CodableFirebase decodes the data and we get our models populated.
var feeds = model.map { $0.value }
//Leaving the keys aside to get the array [FeedsModel]
feeds.sort(by: { (P, Q) -> Bool in P.timestamp > Q.timestamp })
//Sorting the values based on the timestamp, following recent first fashion. It is required because we may have lost the chronological order in the last steps.
if lastKey != nil { feeds = Array(feeds.dropFirst()) }
//Need to remove the first element(Only when the lastKey was not nil) because, it would be the same as the last one in the previous page.
completion(true, feeds)
//We get our data sorted and ready here.
} catch let error {
print("Error occured while decoding - \(error.localizedDescription)")
completion(false, nil)
}
}
}
Now, in our viewController, for the initial load, the function calls go like this in viewDidLoad. And the next pages are fetched when the tableView will display cells...
class FeedsViewController: UIViewController {
//MARK: - Properties
#IBOutlet weak var feedsTableView: UITableView!
var dataArray = [FeedsModel]()
var isFetching = Bool()
var previousKey = String()
var hasFetchedLastPage = Bool()
//MARK: - ViewController LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
//Any other stuffs..
self.getFeedsWith(lastKey: nil) //Initial load.
}
//....
func getFeedsWith(lastKey: String?) {
guard !self.isFetching else {
self.previousKey = ""
return
}
self.isFetching = true
FeedsModel.getFeedsWith(lastKey: lastKey) { (status, data) in
self.isFetching = false
guard status, let feeds = data else {
//Handle errors
return
}
if self.dataArray.isEmpty { //It'd be, when it's the first time.
self.dataArray = feeds
self.feedsTableView.reloadSections(IndexSet(integer: 0), with: .fade)
} else {
self.hasFetchedLastPage = feeds.count < "YOUR FEEDS PER PAGE"
//To make sure if we've fetched the last page and we're in no need to call this function anymore.
self.dataArray += feeds
//Appending the next page's feed. As we're getting the feeds in the recent first manner.
self.feedsTableView.reloadData()
}
}
}
//MARK: - TableView Delegate & DataSource
//....
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if self.dataArray.count - 1 == indexPath.row && !self.hasFetchedLastPage {
let lastKey = self.dataArray[indexPath.row].postId
guard lastKey != self.previousKey else { return }
//Getting the feeds with last element's postId. (postId would be the same as a specific node in YourDatabase/Feeds).
self.getFeedsWith(lastKey: lastKey)
self.previousKey = lastKey ?? ""
}
//....
}