I just learned how to store an array into a Parse Cloud using the example provided by the Parse Documentation:
gameScore.addUniqueObjectsFromArray(["flying", "kungfu"], forKey:"skills")
gameScore.saveInBackground()
Now, utilizing this logic, I want to append strings into the array. So this is what I wrote:
#IBAction func requestButtonPressed(sender: AnyObject) {
var prayerRequests = PFObject(className: "PrayerRequests")
prayerRequests.addObject(["YOIDJFO"], forKey:"skills")
prayerRequests.saveInBackground()
}
Now, after having executed the function requestButtonPressed three times, in parse this is happening:
However. I don't want that to happen when I execute the function requestButtonPressed three times. I want it to be something like this:
Anybody have a solution to this problem?
Every time you use this statement var prayerRequests = PFObject(className: "PrayerRequests") a new PFObject will be created. In order to update a object you need to query the object first and then update its field. In your case you should first get the array by querying for the object, modify / append data to the array and then update the object.
Instead of doing addObject, do insertObject:{yourObject} atIndexPath:{storingPosition} forKey:{#"youKey"}.
And the the value you are adding is an array ["YOIDJFO"] , object should be like {"YOIDJFO"}
Related
How can i add a queryConstraint on a object?
This is my current code but it returns no objects. I guess my current code is actually to query on arrays and not objects. But I can't find a way to do this for objects.
let query = Device.query()
.where(containsString(key: "apps", substring: "Google"))
This is the database
I recommend looking at the playgrounds to see how to use ParseSwift properly. More specifically, finding objects.
The first problem is apps is an object, which is actually a dictionary. You can’t use a substring constraint on a dictionary or other object. The actual way to do it is:
let objectToFind = [“Google”: “300”]
let query = Device.query("apps" == objectToFind),
In Qmetry,Trying to save array of arraylist string and access same in another test case.
Array of array list :
ArrayList<ArrayList<String>> my_list
store(my_list, "array_list_1");
//Accessing saved list
Object list_details = getBundle().getObject(array_list_1);
System.out.println("++++ saved list details++++" + list_details);
I am able to print list_details content. Till this there is no issue. But when I try to get the first index within the arraylist, not able to use .get(0) method. Below is the code.
ArrayList<String> list_details1 = list_details.get(0);
When tried typecasting , got an error 'java.lang.String cannot be cast to java.util.ArrayList'
typecast:
ArrayList<ArrayList<String>>list_details1 = (ArrayList<ArrayList<String>>)list_details;
Need to know is it the right way to store and access arraylist ? Please suggest.
if you want store in file, you can use .csv or .xls to save ArrayList<ArrayList> my_list, if ArrayList<ArrayList> my_list you query from database, you can set as static variable, then you can use in different method
I have two dictionaries. Both declared in a viewController, both based on a model structure class.
// ItemDictionary
var ItemDictionary = ItemModel()
var JSONDictionary = ItemModel()
JSON data is fed into the JSONDictionary and then this data is passed to ItemDictionary which feeds a table within ViewDidLoad.
self.ItemDictionary = self.JSONDictionary
All good. The table is nicely populated from JSON data. I can also delete items from the table and the ItemDictionary. However, when I try and add items back by referring to the original dictionary (JSONDictionary) the item has gone.
I understand this is expected. If Dictionary1 = Dictionary2, a new dictionary is not actually created. Only an second address. So if you change Dictionary1, Dictionary2 also changes.
A practical example could be setting a price range. You can reduce the range and have less items displayed on the table. But you can't replace previously deleted items if you wanted to increase the price range. I do not want to recall the JSON script each time I edit the range. Advice appreciated.
As confirmed by the OP, ItemModel is a class and not a Dictionary. To fix this you need to make ItemModel a real Dictionary and thus a value type. This is probably the preferred choice but will be more work.
An alternative would be to add an convenience initializer to the ItemModel class that instantiates a new copy of itself and call that instead of setting self.ItemDictionary = self.JSONDictionary.
Something like this.
init(model: ItemDictionary) -> ItemDictionary {
// copy properties
}
Then create the new copy.
self.ItemDictionary = ItemDictionary(self.JSONDictionary)
Later you can reinitialize ItemDictionary with the same call.
Try this code out-
var dictionary1 = ["name": "Magnus"]
var dictionary2 = dictionary1
dictionary2.removeAll()
print("\(dictionary2) \(dictionary1)")
The output is :-
[:] ["name": "Magnus"]
Thus 2 new dictionaries are being created. If you refer to the Swift documentation, You will find that in swift, references are hardly present.
Some other portion of code might be responsible. Hope this helps :-)
Problem: I cannot create new PFObjects objectWithoutDataWithClassName:objectId: in swift, is this possible and if so how?
Context: I have a stored array of objectIds of parse objects that I want to use to populate a view but first I have to get each object from parse. Rather than making 'n' number of network requests on parse for each object I want to instead make an array of PFObjects using Parse's objectWithoutDataWithClassName:objectId: functionality and then call [PFObject fetchAllInBackground:block:] with the array. However, in swift it does not appear as though PFObject.objectWithoutDataWithClassname exists. Has anyone encountered this/know how to do this in swift....?
Thanks in advance.
You can use
PFObject(withoutDataWithClassName: <String>, objectId: <String?>)
This method became an initializer.
I am using parse to verify whether a user exists (for example: an other user searches for "userX" and if "userX" exists he will send him a message).
On Parse website they recommend the following code:
var query = PFUser.query()
query.whereKey("gender", equalTo:"female")
var girls = query.findObjects()
Referred to : https://www.parse.com/docs/ios_guide#users-querying/iOS
I am using that code transformed it to:
var query = PFUser.query()
query.whereKey("username", equalTo:typeName.text)
var usernameP = query.findObjects()
But then when i look at println(usernameP) I get an array.
But i want to transform the username ("treert" in this case) into a String that I can put in a label.
Does anybody know how to do that.
Thanks a lot!
As you've discovered, usernameP contains an array. The clue is in the function you've called findObjects (plural). Your query will return all objects where username = typeName.text.
If you only want the first object found, then you will need to call query.getFirstObject().
An alternative is to continue to call findObjects(), but then process the array of results, extracting the object you think is the correct one (assuming the array has more than 0 objects in it), and set the name based on that object.