error creating object using Firebase values - ios

I am attempting to pull down information from my Firebase database and use it to create an object of type Order. The error that I have printed in the catch statement is as follows.
Error Domain=myProjectName.OrderError Code=0 "(null)"
I am unsure what this means exactly, or how to fix it.
I have defined a custom error type in my Order class, as shown below.
enum OrderError: ErrorType
{
case IllegalOrderNumber
case InvalidEntry
}
The error is generated by the following code snippet.
self.ref.child("orders").observeEventType(.ChildAdded, withBlock: { (snapshot) in
let pickupLoc = snapshot.value!["pickupLocation"] as? String
let dropoffLoc = snapshot.value!["dropoffLocation"] as? String
let orderNumInt = snapshot.value!["orderNum"] as? Int
//since the database will return nil if you try and cast a string to an int
//we get it as an int then cast to string
let orderNum = String(orderNumInt)
do
{
let myOrder = try Order(PickUpLoc: pickupLoc, DropOffLoc: dropoffLoc, OrderNum: orderNum)!
self.orders.append(myOrder)
}
catch let error as NSError
{
//should never get here
print(error)
}
})
I do all of the error checking when the user enters the value into the database, so there should be no reason for there to be an error generated.

Okay, so after combing through my code I noticed two primary possible error sources. Firstly, despite the compiler not complaining, the do-catch block was not exhaustive, and therefore an additional catch was added to fix this. Secondly, I believe the error was generated by the fact that I declared my Orders array as var orders = [Order()] as opposed to var orders = [Order](), when I changed this, the program ran smoothly.

Related

Trying to figure out a better way to delete element from Firestore dictionary

I'm using Firestore in my application, and I have a map field called "votes" for user's upvotes or downvotes. It looks like this:
I want to add an option to delete an element from there, this is what I got now:
//getting the user's votes dictionary and removing the post from it.
userRef.getDocument { (doc, error) in
if let _ = error { completion(false) }
guard let dict = doc?.data()?[USER_VOTES] as? [String: Any] else { return }
currentDict = dict
currentDict.removeValue(forKey: id)
}
//setting the votes dictionary with the updated one.
userRef.setData(currentDict) { (error) in
if let _ = error { completion(false) }
else { completion(true) }
}
to me, It looks not really efficient, because each time a user is trying to remove an element from this dictionary, I have to write to the database. which can slow down the process and to my understanding, the free tier of Firestore limits the number of writes.
Is there a better way, maybe deleting it right from the user's document? I tried to look for answers, but couldn't find anything that worked for me.
This one for example: Removing a dictionary element in Firebase looks like what I need to do, but I couldn't get it to work.
EDIT:
I tried deleting it like that
userRef.updateData([
USER_VOTES:[
id: FieldValue.delete()
]
]) { (error) in
if let _ = error { completion(false) }
}
The app crashes says:
Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'FieldValue.delete() can only appear at the top level of your update data
To be able to delete a specific field you should follow the steps mentioned here.
For your case I have created the following under collection 'voting':
So to delete vote2 field you should use:
// Get the `FieldValue` object
let FieldValue = require('firebase-admin').firestore.FieldValue;
// Create a document reference
let fieldRef = db.collection('voting').doc('votes');
// Remove the 'vote2' field from the document 'votes'
let removeField = fieldRef.update({
vote2: FieldValue.delete()
});
And here is the document after running the above:
EDIT :
If the data model is a map inside a document, for example:
Then here is how you can delete a field inside the array which is inside the document:
let docRef = db.collection('voting').doc('user1');
let removeField = docRef.set({'votes':
{['id_vote_1'] : FieldValue.delete()}}, {merge: true});
Here is the document after running the above:

Go-Ethereum iOS cannot unmarshal results

So I'm playing around with go-ethereum in iOS and I'm having quite a bit of trouble trying to interact with a contract deployed to Rinkeby testnet, I'm very new to the whole blockchain technology so any help is appreciated.
All I'm trying to do is access a deployed contract and get the value of a string but the issue I'm having is I get this error when I try to make a call to a bound contract:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=go Code=1 "abi: cannot unmarshal string in to []interface {}" UserInfo={NSLocalizedDescription=abi: cannot unmarshal string in to []interface {}}
this is the code I'm using to make the call.
// Declare the error variables
var clientError: NSErrorPointer;
var addressError: NSErrorPointer;
var contractError: NSErrorPointer;
// Get the bindContract from Rinkeby test network.
let client = GethNewEthereumClient("https://rinkeby.infura.io/v3/398ed56d211646faaf010ca183de11f2", clientError);
let contractAddress = GethNewAddressFromHex("0x7259667715d671Ee370d7788647f95Fe7C3B532d", addressError);
guard let contractABI = ReadJsonResourceAsString(fileName: "InboxContractInterface", fileType: "json") else {
print("[ViewController] failed to read the abi json as string.")
return;
}
let boundContract = GethBindContract(contractAddress, contractABI, client, contractError);
// Prepare the callOpts
let callOpts = GethNewCallOpts();
callOpts?.setGasLimit(300000);
callOpts?.setContext(GethNewContext());
// Prepare the results & params interfaces
let results = GethNewInterfaces(1);
let params = GethNewInterfaces(0);
let stringResult = GethNewInterface();
stringResult?.setDefaultString();
try! results?.set(0, object: stringResult);
// Make the call
let methodName = "message";
try! boundContract?.call(callOpts, out_: results, method: methodName, args: params);
// Show results.
print("[ViewController] message call result: " + (stringResult?.getString())!);
And this is my contract's code:
pragma solidity ^0.4.17;
contract Inbox {
string public message;
function Inbox (string initialMessage) public {
message = initialMessage;
}
function setMessage (string newMessage) public {
message = newMessage;
}
}
For anyone that might find the same issue after digging a bit more I found this issue for android: https://github.com/ethereum/go-ethereum/issues/14832
Luckily this is already fixed, so it was totally my fault for not using the latest version.
I was using Geth v1.5.9 so after updating to v1.8.2 it finally worked, not sure which version in-between got fixed tho.

CloudKit Error Differentiation

I need some help to learn how to properly handle errors when fetching records via CloudKit. Currently I have an app that saves numerous records in the cloud, and will load them at launch. I have been referencing the records using a CKReference, and anytime I save the reference I use the CKReferenceAction.DeleteSelf option. A problem I've encountered periodically is that when a referenced record is deleted, sometimes there can be a significant amount of time before the reference deletes itself. This has caused me to occasionally come across the situation where my app has fetched a CKReference for a record that no longer exists. I'm able to manually find out when this happens just by inserting print(error!) in my error handler. What I would like to know is how I can add some code to detect this specific error i.e. if error.localizedDescription == ??? {.
Here is the basic code I'm using for the fetch:
let fetch = CKFetchRecordsOperation(recordIDs: recordIDs)
fetch.perRecordCompletionBlock = { (record:CKRecord?, recordID:CKRecordID?, error: NSError?) in
if error != nil {
// Error Line A (See below)
print("ERROR! : \(error!.localizedDescription)")
// Error Line B (See below)
print("ERROR: \(error!)")
}
else if let record = record {
// Record was found
}
}
if let database = self.privateDatabase {
fetch.database = database
fetch.start()
}
And then when it tries to fetch the non-existent record, here is the error message that prints out in the compiler window:
a) ERROR! : Error fetching record <CKRecordID: 0x10025b290; dbbda7c3-adcc-4271-848f-6702160ea34f:(_defaultZone:__defaultOwner__)> from server: Record not found
b) ERROR: <CKError 0x125e82820: "Unknown Item" (11/2003); server message = "Record not found"; uuid = (removed); container ID = "(removed)">
Above in error line B, where it says CKError 0x125e82820:, can I use this to create an if statement to check for this specific error type? I really could use any help finding a way to resolve this issue properly when it happens. I have set up some loading structure for my app, and when it thinks there is a record it needs to find, but can't, it screws up my loading process. I would really appreciate any help I can get, I assume it's an easy solution, but apparently not one I've been able to find. Thank you!
UPDATE -
Thanks to #AaronBrager, I was able to find the correct solution. You can verify the error code to match it to any specific error, and the domain to make sure it's a CKError. Here is the solution that works for me:
let fetch = CKFetchRecordsOperation(recordIDs: recordIDs)
fetch.perRecordCompletionBlock = { (record:CKRecord?, recordID:CKRecordID?, error: NSError?) in
if error != nil {
if error!.code == CKErrorCode.UnknownItem.rawValue && error!.domain == CKErrorDomain {
// This works great!
}
}
else if let record = record {
// Record was found
}
}
if let database = self.publicDatabase {
fetch.database = database
fetch.start()
}
You should be able to uniquely identify an error's cause by inspecting its domain and code variables. Same domain and code, same problem. And unlike localizedDescription, it won't change between users.

NSInvalidArgumentException when trying to retrieve images

I am getting an error using a "properly working" code in another place:
[Error]: Caught "NSInvalidArgumentException" with reason "*** -[_NSPlaceholderData
initWithContentsOfFile:options:error:]: nil file argument"
I declared an array of arrays group:[[AnyObject]]
In my CellForRowAtIndexPath method in my UITableView I am starting the following query based on an array which is an element of group => group[indexPath.row].
I can get the necessary data without a problem, but when I try to use my getDataInBackgroundWithBlock() method, it throws the error above.
var memberPhotoImages:[UIImage] = [UIImage]()
let buttonImageQuery = PFUser.query()
buttonImageQuery?.whereKey("objectId", containedIn: group[indexPath.row])
buttonImageQuery?.findObjectsInBackgroundWithBlock({ (results, error) -> Void in
if error != nil {
print(error)
} else {
self.memberPhotosFiles.removeAll()
if let results = results {
for result in results {
let buttonPicture = result["firstImage"] as! PFFile
self.memberPhotosFiles.append(buttonPicture)
buttonPicture.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error != nil {
//...
} else {
if let data = imageData {
print("success!")
}}}}}}})
return cell
}
Any ideas how to solve this? tried a lot of typecasting so far, but it must be something else.
edit: It prints "Success!", but also the error msg.
I had to guess from the error, it looks like you're trying to access a file that doesn't exist on the device -[_NSPlaceholderData initWithContentsOfFile:options:error:]: nil file argument The issue could be that you're accessing something from a place where the simulator can get to it (possibly inside the iOS Simulator's files, which are in Application Support,) but the device cannot. Check your code for any hard-coded paths that might lead to a place on your computer rather than accessing the device's filesystem.
It show that your file argument is nil. Most of such cases are due to different file path between simulator and device. Check your code about loading file and compare the path between simulator and device.

SLRequestHandler error using Swift

I am trying to mimic the app as in this youtube tutorial, but using Swift and I am facing a problem constructing the closure as shown in the code snippet below.
func twitterTimeline() {
let account = ACAccountStore()
let accountType = account.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
// take action
account.requestAccessToAccountsWithType(accountType, options: nil,
completion: { (granted, error) in
if (granted) {
// invoke twitter API
let arrayOfAccount: NSArray = account.accountsWithAccountType(accountType)
if (arrayOfAccount.count > 0) {
let twitterAccount = arrayOfAccount.lastObject as ACAccount
let requestAPI = NSURL.URLWithString("http://api.twitter.com/1.1/atuses/user_timeline.json")
var parameters = Dictionary<String, String>()
parameters["100"] = "count"
parameters["1"] = "include_entities"
let posts = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: SLRequestMethod.GET, URL: requestAPI, parameters: parameters)
posts.account = twitterAccount
// This is the Error Prone Area
let handler: SLRequestHandler = { (response, urlResponse, error) in
self.array = NSJSONSerialization.JSONObjectWithData(data: response, options: NSJSONReadingOptions.MutableLeaves, error: &error) as NSArray
}
posts.performRequestWithHandler(handler)
}
} else {
// do something
}
}
)
}
The error I get is
Cannot convert expression's type '($T1, $T2, $T3) -> $T0' to type '()'
I have tried checking and explicitly casting the types with no much help. I believe the error is somewhere else. Could anyone help me with what exactly is the trouble? I am sorry, if this turns out to be a näive question.
Thanks in advance,
Nikhil
This looks like an interesting case of error propagation in the compiler — I'd suggest filing a bug report with Apple.
The error message you're getting says that you can't assign a closure (which takes three parameters and returns one value) to something that takes no parameters. What's actually going wrong is that the handler closure you're defining takes its error input parameter and tries to pass it to JSONObjectWithData(_:options:error:). That's problematic from a language perspective because the error you're getting in is an immutable (optional) reference to one error, and the parameter you're passing it to expects a mutable pointer for it to (potentially) write another error into.
It's also incorrect API usage. The error you receive as a parameter in the closure is a report of an error that happened in whatever procedure calls your closure. You should log this error, present it to the user, or examine it so your app can gracefully fail. The error parameter you pass to JSONObjectWithData is a place for you to receive reports of additional errors that occur when decoding JSON from your data — you should be handling this error, too. These are two separate places to receive errors, so you shouldn't be passing one to the other.
If you fix that, you'll find a more helpful compiler message saying that the data: label on the first parameter to that function should be omitted. Also, you can use type inference for the options: parameter. So, your handler definition should look something more like this:
let handler: SLRequestHandler = { (response, urlResponse, error) in
// check for error and do something about it if need be, then...
var err: NSError?
if let jsonArray = NSJSONSerialization.JSONObjectWithData(response, options: NSJSONReadingOptions.MutableLeaves, error: &err) as? NSArray {
self.array = jsonArray
} else {
// do something about err
}
}
(You can also probably use a Swift typed array instead of an NSArray if you know what to expect from your JSON. But that's another subject for another question. Actually, several questions.)

Resources