-
Notifications
You must be signed in to change notification settings - Fork 35
Description
π Reproduction steps
I am using two features of sdk-for-apple, TablesDB transactions on upsert of a row, and recently, Storage for associated database image storage. The error "Fatal error: Unexpectedly found nil while unwrapping an Optional value" has popped up in both. It appears that Appwrite is returning nil values for certain items and the sdk expects them to be non-optional.
Here is the relevant code from Sources/AppwriteModels/Row.swift:
public static func from(map: [String: Any] ) -> Row {
return Row(
id: map["$id"] as! String,
sequence: map["$sequence"] as! Int,
tableId: map["$tableId"] as! String,
databaseId: map["$databaseId"] as! String,
createdAt: map["$createdAt"] as! String, <-- error generating line
updatedAt: map["$updatedAt"] as! String, <-- error generating line
permissions: map["$permissions"] as! [String],
data: try! JSONDecoder().decode(T.self, from: JSONSerialization.data(withJSONObject: map["data"] as? [String: Any] ?? map, options: []))
)
}
Here is the relevant code from Sources/AppwriteModels/File.swift:
public static func from(map: [String: Any] ) -> File {
return File(
id: map["$id"] as! String,
bucketId: map["bucketId"] as! String,
createdAt: map["$createdAt"] as! String,
updatedAt: map["$updatedAt"] as! String,
permissions: map["$permissions"] as! [String],
name: map["name"] as! String,
signature: map["signature"] as! String,
mimeType: map["mimeType"] as! String,
sizeOriginal: map["sizeOriginal"] as! Int,
chunksTotal: map["chunksTotal"] as! Int,
chunksUploaded: map["chunksUploaded"] as! Int,
encryption: map["encryption"] as! Bool, <-- error generating line
compression: map["compression"] as! String <-- error generating line
)
}
π Expected behavior
Expected behavior is for it to work and not crash my app.
Note, the Row code identified above only seems to occur when I trigger it as a transaction. If I do a straight in-line upsert, the function completes as expected.
However, I just added the storage.createFile feature yesterday and it crashes on its own, since it does not have a transaction feature.
π Actual Behavior
The app crashes with the above noted error pointing to the lines highlighted above.
In order to get around, I had to manually add the dependency outside of the usual Swift Package Manager routine to manually fix the. code to prevent the errors.
Note, there may be a better fix further back in the code-chain to resolve the issue, but this is what worked for me in the short-term.
For Row, here is my adjusted code on the two lines in question:
createdAt: map["$createdAt"] as? String ?? "",
updatedAt: map["$updatedAt"] as? String ?? "",
For File, here is my adjusted code on the two lines in question:
encryption: map["encryption"] as? Bool ?? false,
compression: map["compression"] as? String ?? ""
Perhaps the fix is to change all func from(map: [String: Any] ) code to be optionals, as I am not sure where else this error may pop up? I know the suggested implications of that across the current 51 models may be monumental. Not sure if base Appwrite server has now changed on how it delivers this information back and is now retuning null values instead of always being non-optional?
π² Appwrite version
Different version (specify in environment)
π» Operating system
Linux
π§± Your Environment
Self-hosted Appwrite version 1.8.1 on Debian VM running on Proxmox
sdk-for-apple version 14.0.0
Note, I see sdk-for-apple is on version 14.3.0. I am not using latest since I had to download the sdk and install instead of using Swift Package Manager due to having to manually edit the source code to avoid the error. When reviewing source code for manual fixes, it appear the same so do not think the latest version addresses the issue.
Hesitant to download latest since I have to manually edit these fixes and not seeing evidence it is fixed in source. But perhaps it has been fixed elsewhere? Please confirm.
Similar but do not appear exactly the same as what I am encountering found at:
#42
#72
#76
It may be related to issue found on sdk-for-android:
appwrite/sdk-for-android#96
π Have you spent some time to check if this issue has been raised before?
- I checked and didn't find similar issue
π’ Have you read the Code of Conduct?
- I have read the Code of Conduct