0

I have a code that saves multiple things into a database (a photo ID, a user ID, a username, a key)

the problem is that when I click the button to save that data Firebase sometimes fails to save all of them (occasionally it fails to save the photo ID or the username or the key or multiple things at a time)

sometimes it succeeds and saves them all to the database

what's the problem and how can I fix it?

ref.child("requests").child("receiver").child((event?.userID)!).child("event").child(event!.key).child("requester").child(user!.uid).child("isRequesterAccepted").setValue(false)
                ref.child("requests").child("receiver").child((event?.userID)!).child("event").child(event!.key).child("requester")
                .child(user!.uid).child("name").setValue(name)
                ref.child("requests").child("receiver").child((event?.userID)!).child("event").child(event!.key).child("requester")
                .child(user!.uid).child("key").setValue(String(user!.uid))
                ref.child("requests").child("receiver").child((event?.userID)!).child("event").child(event!.key).child("requester")
                .child(user!.uid).child("profile_picture").setValue(String(user!.uid))
                ref.child("requests").child("receiver").child((event?.userID)!).child("event").child(event!.key).child("requester")
                .child(user!.uid).child("eventKey").setValue(event?.key)
1
  • try to execute this all in a seprate thread ansync might be that will solve your problem Apr 16, 2019 at 9:50

2 Answers 2

0

I'm not 100% sure, but possible reason is using force unwrap in your code.

So try a little bit refactored one:

guard let indeedEvent = event,
  let indeedUser = user else {
  return
}

ref.child("requests")
  .child("receiver")
  .child(indeedEvent.userID)
  .child("event")
  .child(indeedEvent.key)
  .child("requester")
  .child(indeedUser.uid)
  .child("isRequesterAccepted")
  .setValue(false)

ref.child("requests")
  .child("receiver")
  .child(indeedEvent.userID)
  .child("event")
  .child(indeedEvent.key)
  .child("requester")
  .child(indeedUser.uid)
  .child("name")
  .setValue(name)

ref.child("requests")
  .child("receiver")
  .child(indeedEvent.userID)
  .child("event")
  .child(indeedEvent.key)
  .child("requester")
  .child(indeedUser.uid)
  .child("key")
  .setValue(String(indeedUser.uid))

ref.child("requests")
  .child("receiver")
  .child(indeedEvent.userID)
  .child("event")
  .child(indeedEvent.key)
  .child("requester")
  .child(indeedUser.uid)
  .child("profile_picture")
  .setValue(String(indeedUser.uid))

ref.child("requests")
  .child("receiver")
  .child(indeedEvent.userID)
  .child("event")
  .child(indeedEvent.key)
  .child("requester")
  .child(indeedUser.uid)
  .child("eventKey")
  .setValue(indeedEvent.key)
0

I'm not sure if this it the 'right' answer but maybe it will lead you to one.

A few things:

You can greatly simplify that code to this

let ref = rootRef.child("requests").child("receiver").child((event?.userID)!).child("event").child(event!.key).child("requester").child(user!.uid)
let data = [
    "isRequesterAccepted": false,
    "key": String(user!.uid),
    "profile_picture": String(user!.uid),
    "eventKey": event?.key
]
ref.setValue(data)

also, as mentioned, you're not safely unwrapping optionals so it may be that data is invalid or not what is expected.

This for example is dangerous

(event?.userID)!

as event could be nil and that would cause the app to crash.

Also, this could be an issue and you are stating here, that event, for certain is not nil.

event!.key

I also noted that you storing the same value in two nodes - that may be an issue

.child("key").setValue(String(user!.uid))
.child("profile_picture").setValue(String(user!.uid))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.