Get data from a pointer's row in Parse (ios) - ios

I have a pointer named "Parent" in my class, "ComparablePhotos", which points to another class, "NewLog". I am trying to get the data (Address, ObjectId, Lat) from the row in NewLog that Parent points to. How do I do this using query1.includeKey provided?
Code:
var query1 = PFQuery(className: "ComparablePhotos")
query1.includeKey("Parent")
//get NewLog[address], NewLog[lat], and NewLog[objectId] using "Parent" pointer
ComparablePhotos class:
NewLog class:

see if this helps you out at all...you probably will have to change some of the objects to exactly fit your words from the parse database.hopefully that will steer you in the right direction
var query1 = PFQuery(className:"NewLog")
query1.includeKey("Parent")
query1.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
var address = object["address"] as! String
var lat = object["lat"] as! String
var objectId = object["objectId"] as! String
this is how i query a role, which is similar to a pointer query...
var roleQuery = PFRole.query()
roleQuery!.whereKey("name", equalTo: "admin")
roleQuery!.getFirstObjectInBackgroundWithBlock() { (roleObject: PFObject?, error) -> Void in
var adminRole = roleObject as! PFRole
adminRole.users.query()!.findObjectsInBackgroundWithBlock({ (users, error:NSError?) -> Void in
if error == nil {
if let adminUsers = (users as? [PFUser]) {

Related

How to get multiple objects from Parse to save in Local DataStorage in Swift?

I'm getting objects from Parse and display it in my UI. Now I am working on saving the data in Local DataStorage of Parse. I looked at the following Parse example:
let query = PFQuery(className: "GameScore")
query.fromLocalDatastore()
query.getObjectInBackgroundWithId("xWMyZ4YE").continueWithBlock {
(task: BFTask!) -> AnyObject in
if let error = task.error {
// Something went wrong.
return task;
}
// task.result will be your game score
return task;
}
The above example if for fetching 1 object. I dont know how to do the same for multiple objects. I am fetching the objects through MY following code:
let query:PFQuery = PFQuery(className: "Events")
query.findObjectsInBackgroundWithBlock {
(object, error) -> Void in
if object != nil
{
if(object!.count != 0)
{
for messageObject in object! {
let eventName:String? = (messageObject as! PFObject)["EventName"] as? String
let createdBy:String? = (messageObject as! PFObject)["CreatedBy"] as? String
let eventDate:String? = (messageObject as! PFObject)["EventDate"] as? String
objModalClass.eveName = eventName!
objModalClass.crtedBy = createdBy!
objModalClass.eveVenue = eventVenue!
}
}
}
}
In my above code, how can I save all the fetched objects in objModalClass in Local DataStorage. Kindly explain in detail.
You should look object pinning. As long as you have enabled the local datastore, you can retrieve objects by a pin name, as well as releasing those objects from the local datastore with unpinning. From the docs:
"Asynchronously stores the object and every object it points to in the local datastore, recursively."
For single objects:
public func pinInBackgroundWithName(name: String, block:PFBooleanResultBlock?)
For many objects:
public class func pinAllInBackground(objects: [AnyObject]?, withName name: String, block: PFBooleanResultBlock?)
The complementary unpinWithName(name: String) is available for both scenarios. Pinning only works with PFObjects and its subclasses.
You can also query from a pin name like so
var query = PFQuery.queryWithClassname("PFObject_subclass")
query.fromPinWithName("Your_given_pin_name")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
//objects from pin name returned.
}

how to retrive data from a user pointer on Parse using swift

On Parse I have users with Facebook profile and Email login profile. So I want to bury for users data in my twitter-like app.
In my "messages" class on Parse I have column "sender" that contains pointers to parse users.
I just want to retrive and show the name of users in class "messages" contained in the column "sender" wich contains pointers to PFUsers of which I need data for keys
"first_name"
"last_name"
"profile_picture"
How can I retrive their data like name and image in order to show them in a tableview?
these are the declarations of my arrays:
var sendersArray : [String] = []
var picturesArray : [NSData] = []
maybe I could use something like this tuple, but I can't understand how to grab data from pointers
for user in list {
let firstName = "fist_name"
let lastName = "last_name"
let oProfileImage = NSData() //"image_profile" as! NSData
otherUsers.append((oName: firstName, oLastName: lastName, oImageProfle: oProfileImage))
}
version - 1:
I started with printing the whole pf object
//******************************************************
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.sender!
print(object)
print(theName)
sendersArray.append(theName)
let profilePicture = object["profile_pic"] as! PFFile
picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
//*******************************************************
version - 2:
then, found this solution, but still, doesn't
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages" )
theSearchQuery.includeKey("sender")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.sender!["first_name"] as? String
print(object)
print(theName)
sendersArray.append(theName)
let profilePicture = object["profile_pic"] as! PFFile
picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
errors:
seems to be a problem with sender, maybe I shouldn't use it
thanks in advance
let theName = object.objectForKey("sender")!.objectForKey("first_name") as! String
Complete Code:
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages")
theSearchQuery.includeKey("sender")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.objectForKey("sender")!.objectForKey("first_name") as! String
print(object)
print(theName)
self.sendersArray.append(theName)
let profilePicture = object["profile_picture"] as! PFFile
self.picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
Also, your picturesArray should be of type PFFile, like this:
var picturesArray = [PFFile]()
NOT NSData. change that at the top of your class.
-----EDIT------:
If you want to retrieve an image from a parse query, do this:
1) at the top of your class, declare the following arrays to store the results:
// your images will be stored in the file array
var fileArray = [PFFile]()
// your first and last names will be stored in String Arrays:
var firstNameArray = [String]()
var lastNameArray = [String]()
2) perform the query:
let query1 = PFQuery(className: "_User")
query1.orderByDescending("createdAt")
query1.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
if error == nil {
for x in objects! {
let firstName = x.objectForKey("first_name") as! String
let lastName = x.objectForKey("last_name") as! String
self.firstNameArray.append(firstName)
self.lastNameArray.append(lastName)
if x.objectForKey("profile_picture") as? PFFile == nil {
print("do nothing cause it's nil")
}
else {
let file:PFFile = x.objectForKey("profile_image") as! PFFile
self.fileArray.append(file)
}
}
self.tableView.reloadData()
}
})
Note I am using Swift 2 and Xcode 7. Syntax is slightly different in Xcode 6.4 and Swift 1.2.

Query on PFUser according to key value (Swift)

I have PFUser saved as a pointer in this class. I'd like to retrieve the user's first name and corresponding "point value"
My attempt below to append that data to it's cell value, but it is only returning the last retrieved object for that key value.
var innerQuery : PFQuery = PFUser.query()!
innerQuery.whereKeyExists("objectId")
let query = PFQuery(className: "myClass")
query.whereKey("userId", matchesQuery: innerQuery)
query.whereKey("points", greaterThan: 1000)
query.findObjectsInBackgroundWithBlock{ (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = query.findObjects() as? [PFObject]{
for object in objects {
if let listPoints = object.objectForKey("points") as? Int {
var temp = String(listPoints)
cell.pointStatus.text = temp
}
}
}
}
else{
println(error?.description)
}
}
I retrieve the users first name and profile picture in a separate call. Everything is functional aside from the query for points.
if let pfuser = userProfile["first_name"] as? String{
if let pfimage = userProfile["profile_picture"] as? PFFile{
pfimage.getDataInBackgroundWithBlock({
(result, error) in
cell.userIcon.image = UIImage(data: result!)
cell.userName.text = username
})
}
}
It appears that you are only writing the value of listPoints to one cell in
var temp = String(listPoints)
cell.pointStatus.text = temp
If you want to have multiple point values to be displayed, the cell reference will need to be changed.

Convert String to PFObject.. Parse

I am retrieving all the user details this way..
var userIds = [String]()
var userNames = [String]()
var profilePics = [PFFile]()
var gender = [String]()
override func viewDidLoad() {
super.viewDidLoad()
var userQuery = PFUser.query()
userQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
self.userIds.removeAll(keepCapacity: true)
self.userNames.removeAll(keepCapacity: true)
self.profilePics.removeAll(keepCapacity: true)
for object in objects {
if let user = object as? PFUser {
if user.objectId != PFUser.currentUser()?.objectId {
self.userIds.append(object.objectId as String!)
self.userNames.append(object["fullName"] as! String!)
self.profilePics.append(object["profilePicture"] as! PFFile!)
self.gender.append(object["gender"] as! String!)
}
}
}
}
self.tableView.reloadData()
})
}
now i have "follow" button in myCell.. saving this way..
#IBAction func followButtonTapped(sender: UIButton) {
var followers:PFObject = PFObject(className: "Followers")
followers["user"] = userIds[sender.tag] // Problem is here.. i want "user" column to be filled with userIds of users as Pointers not Strings. What should i do here??
followers["follower"] = PFUser.currentUser()
followers.saveInBackground()
}
Because i want to make relations with every object in "Followers" class that's why i want them to be in the form of Pointers.. Any suggestion please??
If you want to create a pointer to another PFObject (including PFUser) you will need to actually point to that object.
I see in your query that you are not adding any PFUsers to your array, but you are getting their object IDs.
Since a tag is only for an Int, you can not use it to pass a String (which a Parse objectID is).
You can subclass UIButton and create a new property to handle the objectID from parse and then use that to perform a query with the objectID and save the pointer to the result of that query. Or you can just add the object from your first query to your local array and use your button's tag to get the index of the object you want to point to.
Update
Since you have the objectId for the PFuser you want, you can get it and update your followers like so:
let getOjbectByIdQuery = PFQuery(className: "User")
getOjbectByIdQuery.whereKey("objectId", equalTo: userIds[sender.tag])
getOjbectByIdQuery.getFirstObjectInBackgroundWithBlock { (foundObject: PFObject?, error: NSError?) -> Void in
if let object = foundObject {
var followers:PFObject = PFObject(className: "Followers")
followers["user"] = object
followers["follower"] = PFUser.currentUser()
follwers.saveInBackground()
}
}

Query Pointer in Parse (ios)

I am doing a query and I am checking to see if the value in the column "Parent", which is a pointer, is equal to a string, newLogObjectId. I obviously cannot do this since a pointer and a string are a different value type, (returns nil). How do I compare a string to a string in a pointer?
//A string, for example, "MCeKMyxRIt"
let newLogObjectId = objectIdArray[markerIndex]
let query1 = PFQuery(className: "ComparablePhotos")
//"Parent" is a pointer referencing an objectId in another class. "newLogObjectId" is a string How do I check to see if the String of Parent is equal to newLogObjectId, a string?
query1.whereKey("Parent", equalTo:newLogObjectId)
Pointers in Parse don't point to a value, they point to a class (a PFObject). So it looks like your pointer named Parent is pointing to the Parse class NewLog. I'm assuming then the string you are wanting to check is a field in the class NewLog. Also, to include the pointer in the query, use query1.includeKey("PointerName"). Try this:
let newLogObjectId = objectIdArray[markerIndex]
let query1 = PFQuery(className: "ComparablePhotos")
query1.includeKey("Parent")
query1.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in
if (error == nil){
if let comparablePhotos = objects as? [PFObject]{
for photo in comparablePhotos {
if let parentPointer:PFObject = photo["Parent"] as? PFObject{
if(parentPointer["columnWithString"] as! String == newLogObjectID){
// Do something
}
}
}
}
}else{
println(error)
}
})

Resources