Getting NSInvalidArgumentException when posting a Dictionary to Firestore - swift

I'm trying to add an array of custom objects to a Firestore Database. The Dictionary looks as follows:
for (n, eventInArray) in currentEvents.enumerated() {
ref.updateData(["Events":
[n:
["activity": eventInArray.activity],
["dateAndTime"]: ["date": eventInArray.dateAndTime.date,
"time": eventInArray.dateAndTime.time],
"name": eventInArray.name
]])}
But when running the app and sending the data, I get following error message:
NSInvalidArgumentException', reason: '-[__NSCFNumber getBytes:maxLength:usedLength:encoding:options:range:remainingRange:]: unrecognized selector sent to instance 0xd628e63903198e1c'

I figured out a way to work around this issue.
Instead of updating one field, I updated the whole document using a do - catch block:
do {
try ref.setData(from: groupDataForDb)
} catch let error {
print("Error writing to firestore \(error)")
}
The groupDataForDb variable is a struct that conforms to Codable, and has properties matching the fields in the Firestore Document.

Related

Swift Firebase Functions ERROR: NSCocoaErrorDomain

When I try to run a Firebase Function from Swift, I get an error saying:
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.}
This is the Firebase Function:
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!")
})
This is my Swift code:
Functions.functions().httpsCallable("helloWorld").call { (result, error) in
if error == nil {
print("Success: \(result?.data)")
} else {
print(error.debugDescription)
}
}
You may see this error if you haven't specified the correct region on the client side. For example:
lazy var functions = Functions.functions(region: "europe-west1")
My issue was that I used http://localhost:5000 instead of the correct http://localhost:5001. The emulator by default emulates the hosted website at port 5000 and the functions at port 5001.
I realized this by evaluating the response in FIRFunctions.m#261. It was the html of the hosted website which obviously cannot be parsed as json.
response.send("Hello from Firebase!")
You are not returning a JSON TEXT here but you are returning a string.
You need to return something like
response.send('{"message": "Hello from Firebase!"}')

How do you make a storage reference in swift for Firestore

How do you create a field of type storage reference in the swift dictionary format using swift. I keep on getting an error saying that FIRStorageReference.
This is the code below, photoRef is a storage reference:
let newFirePhotoRef = newUserRef.collection("Photos").document()
photoRef = photoRef.child(newFirePhotoRef.documentID)
newFirePhotoRef.setData([
"Date":Date(),
"Photo Reference in Storage": photoRef
])
This is the error I receive:
Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: FIRStorageReference (found in field Photo Reference in Storage)'

Realm write fails on second time being called

I have a service for retrieving data from an api, it returns json that I map and then populate to Realm and finally display this to the view.
I have way for a user to force refresh the retrieval of data, which means I need to update my Realm data aswell. This works fine on the first time I call the method. But if I try to do it again it crashes every single time with this exception.
*** Terminating app due to uncaught exception 'RLMException', reason: 'Can only add, remove, or create objects in a Realm in a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
This is how my function works:
private func writeCollection(someKey: String?) {
let realm = try! Realm()
try! realm.write {
realm.add(someObject[someKey!]!, update: true)
}
}
This error occurred inside a different function then initially thought. I was holding a dictionary like this [someKey:someObject] and using this dictionary as a reference to write from.
Problem was this was outside the try! realm.write
// removed this reference
// var dictionary = [String:SomeObject]()
realm.write {
let someObject = SomeObject()
someObject.id = someKey
realm.add(someObject, update: true)
}

Deleting object from parse-server code causing error?

I am developing an app in Xcode using swift and am using a heroku-hosted parse-server as my database. I want to be able to delete an object from the database, but I keep getting an error when trying to type out the code. Here is what I have:
{
let removingObjectQuery = PFQuery(className: "GoingTo")
removingObjectQuery.whereKey("objectId", equalTo: goingToSelectionID)
removingObjectQuery.findObjectsInBackground(block: { (object, error) in
if let objects = object{
print("Object found")
for object in objects{
object.deleteInBackground()
}
}
})
}
But the delete .deleteInBackground keeps sending an error to in the code line saying that ".deleteInBackground is not a member of [PFObject]"... except that I thought it is a member of that value type?
Edit: Syntax fixed to allow calling of .deleteInBackground but now am receiving error in logs (which does not crash the app) that "[Error]: Object not found". The object is for sure in the DB and whereKey equalTo: is adequately described... (goingToSelectionID is indeed the objectId in the DB... checked this by printing to logs). Not sure what is wrong?
The findObjectsInBackground method doesn't return results of type PFObject, but [PFObject], which is an array of PFObjects... If you want to delete the whole array, you can use the class method deleteAllInBackground like so:
PFObject.deleteAllInBackground(objectt, block: nil)
Or you can iterate through the array:
for objectt in object! {
objectt.deleteInBackground()
}

Why am I getting this error during Realm migration?: 'RLMException', reason: 'Invalid value for property'

I am doing a migration, in Realm, to convert a string to an array.
Here is my code:
migration.enumerateObjects(Word.className()) { oldObject, newObject in
var defString = oldObject["string"] as String
var defArray: [String] = defString.componentsSeparatedByString("/")
println(defArray) // [variant of 籲|吁[yu4]]
newObject["array"] = defArray
}
When I run the migration it displays this error: 'RLMException', reason: 'Invalid value for property'
Here is the string that is throwing the exception: /variant of 籲|吁[yu4]/
I have tried removing the brackets and the pipe, but it still doesn't work. I am not sure if it is because of this particular string or if it has to do with some sort of incorrect type.
Any suggestions?
Realm doesn't support storing properties of Swift's Array type. Realm does support RLMArray properties, whose items must instances of an RLMObject subclass.
See Realm's documentation on Models or on RLMArrays for more information.