Why am I getting the pointer error? - ios

In the user class I have a pointer "Friendship" to the user class, but when I run the program I get the error -
EDIT: I have figured out why I get the error. "Frinendship is not a pointer, it is a PFRelation, but I still want to access the ProPic and username. How would I do this?
Below is the user class
Below is the Pointer to the user class.
func getFriendPic(){
let imagequery = PFQuery(className: "_User")
imagequery.whereKey("username", equalTo: PFUser.currentUser()!)
imagequery.includeKey("Friendship")
imagequery.findObjectsInBackgroundWithBlock {( objects: [AnyObject]?, error: NSError?) -> Void in
for object in objects!{
let userPic = object["ProPic"] as! PFFile
userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if(error == nil){
let image = UIImage(data: imageData!)
self.arrayOfFriends.append(image!)
print(self.arrayOfFriends)
}
// dispatch_async(dispatch_get_main_queue()) {
// self.collectionView.reloadData()
// }
})
}
}
}
func getFriendName(){
var query = PFQuery(className: "_User")
query.whereKey("username", equalTo: PFUser.currentUser()!)
query.includeKey("Friendship")
query.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]?, error: NSError?) -> Void in
var objectIDs = objects as! [PFObject]
for i in 0...objectIDs.count-1{
self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
print(self.arrayOfFriendsNames)
}
})
}

The include query parameter doesn't work with Relation. Once you retrieved your user, you need to execute a second query to get the Friendship.
Here's an example to retrieve a PFRelation:
var relation = user.relationForKey("Friendship")
relation.query().findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if let error = error {
// There was an error
} else {
// objects contains the friendships of the user
}
}

Related

Parse getting images from server (getDataInBackground not working)

I am new to coding.
Trying to append images from Parse to a [UIImage] but get the error "Cannot convert value of type PFFileObject to expected argument type UIImage.
How can I convert the PFFile to a UIImage?
#Published var profileManageImages = [UIImage]()
ForEach(uploadMedia.profileManageImages, id: \.self) { picture in
Image(picture)
func refreshSideScroll() {
let currentUser = PFUser.current()
let query = PFQuery(className: "Photos")
query.whereKey("uploadedBy", equalTo: currentUser!)
query.limit = 8
query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil {
for images in objects!{
self.profileManageImages.append(images["image"] as! PFFileObject)
}
}
})
}
You need still to fetch data of image from PFFileObject, like
query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil, let objects = objects {
for object in objects {
if let file = object["image"] as? PFFileObject {
file.getDataInBackgroundWithBlock{(data: NSData?, error: NSError?) in
if error == nil, let data = data, let image = UIImage(data: data) {
DispatchQueue.main.async {
self.profileManageImages.append(image)
}
}
}
}
}
})

How do I get the array to append the objects in the database in order?

I am appending the username column and the image column to two different arrays from parse. I am then putting them into the collection view. I am anticipating that the username in the nameArray corresponds to the imageArray, but majority of the time they are in the wrong order. How do I get them to append into the array in the right order? i.e. User 1 has picture 1, user 2 has picture 2. username array = [User 1, User 2]. image array = [picture 1, picture 2].
func getFriendPicandName(){
let imagequery = PFQuery(className: "_User")
imagequery.findObjectsInBackgroundWithBlock {( objects: [AnyObject]?, error: NSError?) -> Void in
// for object in objects!{
var user = PFUser.currentUser()
let relation = user!.relationForKey("Friendship")
relation.query()!.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
for object in objects!{
let userPic = object["ProPic"] as! PFFile
userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if(error == nil){
let image = UIImage(data: imageData!)
self.arrayOfFriends.append(image!)
print(self.arrayOfFriends)
}
dispatch_async(dispatch_get_main_queue()) {
self.collectionView.reloadData()
}
})
}
}
}
var query = PFQuery(className: "_User")
query.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]?, error: NSError?) -> Void in
var user = PFUser.currentUser()
let relations = user!.relationForKey("Friendship")
relations.query()!.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]?, error: NSError?) -> Void in
var objectIDs = objects as! [PFObject]
for i in 0...(objectIDs.count){
self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
print(self.arrayOfFriendsNames)
}
dispatch_async(dispatch_get_main_queue()) {
self.collectionView.reloadData()
}
}
})
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayOfFriendsNames.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView
cell.friendname.text = arrayOfFriendsNames[indexPath.item]
cell.friendpic.image = arrayOfFriends[indexPath.item]
cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
cell.friendpic.clipsToBounds = true
return cell
}
You should not call your query twice, I would imagine your for loop to look something like this:
for object in objects!{
let userPic = object["ProPic"] as! PFFile
userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if(error == nil){
let image = UIImage(data: imageData!)
self.arrayOfFriends.append(image!) // Add image here
print(self.arrayOfFriends)
}
self.arrayOfFriendsNames.append(object.valueForKey("username") as! String) // Add Name here
}
I don't know much about swift but I combined you both function into one...i hope it will help you...
func getFriendPic(){
let imagequery = PFQuery(className: "_User")
imagequery.findObjectsInBackgroundWithBlock {( objects: [AnyObject]?, error: NSError?) -> Void in
// for object in objects!{
var user = PFUser.currentUser()
let relation = user!.relationForKey("Friendship")
relation.query()!.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
var objectIDs = objects as! [PFObject]
for i in 0...(objectIDs.count){
self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
print(self.arrayOfFriendsNames)
}
for object in objects!{
let userPic = object["ProPic"] as! PFFile
userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if(error == nil){
let image = UIImage(data: imageData!)
self.arrayOfFriends.append(image!)
print(self.arrayOfFriends)
}
dispatch_async(dispatch_get_main_queue()) {
self.collectionView.reloadData()
}
})
}
}
}
}

swift/parse getObjectInBackgroundWithId query not working

I'm trying to use the getObjectInBackgroundWithId method, of a PFObject and it throws this error:
"Cannot invoke 'getObjectInBackgroundWithId' with an argument list of type (string, block: (PFObject!,NSError?) -> Void"
I have written the below code:
var result = PFQuery(className: "posts")
result.getObjectInBackgroundWithId("kk", block: {
(object: PFObject, error: NSError?) -> Void in
object["pLikes"] = object["pLikes"] + 1
object.save()
})
Any help?
You have to use getObjectInBackgroundWithId with
let query = PFQuery(className: "posts")
query.getObjectInBackgroundWithId("kk") { (objects, error) -> Void in
}
OR
let query = PFQuery(className: "posts")
query.getObjectInBackgroundWithId("kk") { (objects:PFObject?, error:NSError?) -> Void in
}
Change the object
let query = PFQuery(className: "posts")
query.getObjectInBackgroundWithId("kk") { (objects, error) -> Void in
let testObj = objects?.first as! PFObject
testObj.setObject(26, forKey: "pLikes")
testObj.saveInBackgroundWithBlock { (succeeded, error) -> Void in
if succeeded {
println("Object Uploaded")
} else {
println("Error: \(error) \(error!.userInfo!)")
}
}
}

Fatal error when trying to load images from parse using Swift

I am getting a fatal error: unexpectedly found nil while unwrapping an optional value. I get this error after the code gets to self.imageArray.append(image!) from this code:
func retrieveImages()
{
var query = PFQuery(className: "Maps")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
let objects = objects as! [PFObject]
for object in objects {
let imageFile = object["imageFile"] as! PFFile
imageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if imageData != nil {
let imageData:NSData! = imageData
let image = UIImage(data: imageData)
self.imageArray.append(image!)
}
}
}
}
}
}
}
I have checked each line of code and image does not become nil until i try to append it to the end of the imageArray.
like previous mentioned, you are reassigning a lot of variables. Try you if-let statements on the optional variables like this:
func retrieveImages(){
var query = PFQuery(className: "Maps")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) in
if let objects = objects as? [PFObject] where error == nil {
for object in objects {
if let imageFile = object["imageFile"] as? PFFile {
imageFile.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) in
if let imageData = imageData, let image = UIImage(data: imageData) {
self.imageArray.append(image)
}
}
}
}
}
}
}

Missing argument for parameter #1 in call when using findObjectsInBackgroundWithTarget

I am trying to get an array of PFObjects, [PFObject], from Parse and seeing the issue below. What am I missing?
Error is Missing argument for parameter #1 in call
func loadData() {
rooms = [PFObject]()
users = [PFUser] ()
self.tableView.reloadData()
let pred = NSPredicate(format: "user1 = %# OR user2 = %#", PFUser.currentUser()!, PFUser.currentUser()!)
let roomQuery = PFQuery(className: "Rooms", predicate: pred)
//gives us all the information - includeKey all columns for the user class itself
roomQuery.includeKey("user1")
roomQuery.includeKey("user2")
roomQuery.findObjectsInBackgroundWithTarget{ (results: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
self.rooms = results as [PFObject]
for room in self.rooms {
let user1 = room.objectForKey("user1") as PFUser
let user2 = room.objectForKey("user2") as PFUser
if user1.objectId != PFUser.currentUser() {
self.users.append(user1)
}
if user2.objectId != PFUser.currentUser() {
self.users.append(user2)
}
}
self.tableView.reloadData()
}
}
}
Error:
It seems that you want to handle the response with a block but you decided to use findObjectsInBackgroundWithTarget. You should go with findObjectsInBackgroundWithBlock:. So you should be having something like this:
roomQuery.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in

Resources