in my app, I am trying to decode data from the firebase document to the User object but I am receiving the next code error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
the app does not crush, so it keeps in a loop there.
The app is built with MVVM - service - repository pattern and the problem could be the nested completions or the background call to firebase, I don't know it.
The ViewModel calls the service layer where the business logic is implemented, and the service call to the repository layer brings the data from firebase. the code is the following bellow:
func loadAllUsers(completion : @escaping (Result<[User], UserRepositoryError>) -> Void) {
usersRef.getDocuments { snapshot, error in
if let error = error {
print( "debug: Error getting documents: \(error.localizedDescription)")
return
}
guard let documents = snapshot?.documents else {
print( "debug: No documents found")
return
}
var users = [User]()
for document in documents {
let id = document.documentID
let data = document.data()
let location: GeoPoint = data["location"] as! GeoPoint
let name = data["name"] as! String
let profileImageData = data["profileImageData"] as! Data
let surname = data["surname"] as! String
let products: [Product] = data["products"] as! [Product]
let user = User(id:id ,name: name, surname: surname, products: products, location: location, profileImageData: profileImageData)
users.append(user)
}
completion(.success(users))
}
}
this is not a good way to decode a document but in this way we can see with more details where the code fails, right now I´m getting the same issue at:
let data = document.data()
