Value of optional type PFQuery? not unwrappped - swift

I updated to the latest version of Swift/XCode and a PFQuery in my app is generating an error: Value of optional type 'PFQuery?' not unwrapped. I know I could add a bang (!) but this only makes the error goes away. It doesn't actually fix the problem. The query used to return results before I upgraded. Here's the code in question:
PFGeoPoint.geoPointForCurrentLocationInBackground { (geopoint, error) -> Void in
println(error)
if error == nil {
println(geopoint)
if var user = PFUser.currentUser(){
user["location"] = geopoint
var query = PFUser.query()
query.whereKey("location", nearGeoPoint:geopoint) //error on this line
query.limit = 10
query.findObjectsInBackgroundWithBlock({ (users, error) -> Void in
Instead of simply making it query!.whereKey(etc) what's the best way to fix this?
Thanks!

Thank you for asking, as it is all too tempting just to force-unwrap. Do it like this:
if var query = PFUser.query() {
query.whereKey("location", nearGeoPoint:geopoint)
// ... and so on
}
(It is odd that you do not know this, since you are doing it correctly just two lines before with PFUser.currentUser(). A case of the left brain not knowing what the right brain is doing?)

Related

Cannot assign result of Parse PFQuery to instance variable of my controller Class in Swift

I'm trying to assign the value returned from the result of a Parse query to an instance variable of my view controller class called "currentProfile". Although the function retrieves the data from the server okay, it seems like it doesn't assign it to my instance variable.
This is my code :
let query = PFUser.query()
query!.getObjectInBackgroundWithId(self.profileId) {
(profile: PFObject?, error: NSError?) -> Void in
if error == nil && profile != nil {
print(profile)
self.currentProfile = (profile as? PFUser)!
} else {
print(error)
}
}
print(currentProfile)
So when I print profile the first time it prints it correctly, however when I print currentProfile outside the function it actually doesn't print anything.
If you have any idea why this is happening or know how could I fix it, it would be greatly appreciated if you could let me know.
Thanks.

pointers in parse class with swift code

I'm trying to insert a text under description column, and it keep giving me error, I think I'm having this issue because I wasn't able to use the pointer properly.. is there any one can help, I watched a number of tutorial video's as to how to setup the pointer in Parse, and that didn't solve the issue.
Thanks.
let me = self.textField.text
let query = PFQuery(className: "Store")
query.getObjectInBackgroundWithId ("product", block: {
(object: PFObject?, error: NSError?) -> Void in
if error != nil
{
print(error)
}
else if let transaction = object {
transaction["discription"] = "\(me)"
transaction.saveInBackground()
print(object!.objectForKey("discription"))
}
})
I finally got the answer, the best possible answer I got was, not store any information to the DB till all the input is done from the last page. It means you carry over the input as temp to each pages, then save it at the end... :)

Compound Query from loop - Parse Swift

In the app Im working on, users select a set of "tags" among a list, then I need to query all items which include at least one of those tags.
To do so, I want to use compound queries.
documentation from Parse.com
var lotsOfWins = PFQuery(className:"Player")
lotsOfWins.whereKey("wins", greaterThan:150)
var fewWins = PFQuery(className:"Player")
fewWins.whereKey("wins", lessThan:5)
var query = PFQuery.orQueryWithSubqueries([lotsOfWins, fewWins])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains players with lots of wins or only a few wins.
}
}
my issue here is that my tags are in an array, and I don't know how much there is (could be 1, could be 3). So I though about doing a for loop to create a query for every one of them.
my problem is that I can't access the variable outside of this for loop, and I can't declare them before hand as I don't know how much there will be.
something that would look like this.
for(var i = 0; i < tags.count; i++){
let i = PFQuery(className: "items")
i.whereKey("tags", equalTo: tags[i])
}
var query = PFQuery.orQueryWithSubqueries([variable1, variable2 ... ])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results
}
}
is there a way to achieve such thing?

ObjectIDQuery.findObjectsInBackgroundWithBlock causing Xcode source editor to have limited functionality

I am attempting a query from Parse to get the object ids in an array and not have to hard code the ids. I attempt to use the following code:
var ObjectIDQuery = PFQuery(className: "QuestionsandAnswers")
ObjectIDQuery.findObjectsInBackgroundWithBlock({
(objectsArray : [AnyObject]?, error : NSError?) -> Void in
var ObjectIDs = objectsArray as! [PFObject]
for i in 0..<ObjectIDs.count{
self.ObjectIDsPublicArray.append(ObjectIDs[i].objectId)
}
})
But the code causes Xcode to state "Xcode encountered a problem. Source editor functionality is limited.Attempting to restore"
Anyone know why that code would cause that? Also any suggestions to fix?
you are on the right path but Parse doesn't use [AnyObject]? anymore in their new SDK so change to [PFObject]?
Example:
let objectIdQuery = PFQuery(className: "QuestionsandAnswers")
objectIdQuery.findObjectsInBackgroundWithBlock({
(objectsArray : [PFObject]?, error : NSError?) -> Void in
if error == nil
{
if let objects = objectsArray
{
for one in objects
{
let objectID = one.objectID //<--- objectID
// then append the objectID into your data structure
}
}
}
})
This is a common problem, basically parse updated their SDK
Just change
[AnyObject]? to [PFObject]?
Same problem here I think
PFArrayResultBlock(parse) is causing an error while converting to swift 2.0

How to properly Pin in Parse localDataStore?

I have Parse.enableLocalDatastore() in my app delegate before Parse.setApplicationId
Then I have var newPosts = PFObject(className: "Post") as a global variable.
Then I want to get 1,000 latest objects from the "Post" table from localDataStore that I enabled earlier so I do this:
var getNewPosts = PFQuery(className: "Post")
getNewPosts.fromLocalDatastore()
getNewPosts.limit = 1000
getNewPosts.orderByDescending("createdAt")
getNewPosts.findObjectsInBackgroundWithBlock {
(downloadedPosts, error) -> Void in
if downloadedPosts != nil && error == nil {
println(downloadedPosts) // I am getting table with no data
}
}
But I only get empty data rows.
If I comment out the getNewPosts.fromLocalDatastore() line results are fine.
I understand that I am missing the critical Pinning step but not sure from Parse documentation hoe and where to implement it. Can you please help?
You are getting no data....
reasons my be...
Wrong name of class (class names are case sensitive)
Data is not there in local storage
Try synchronous version of findObject: method and pin: method.