mark this question as solved quesstion apples ios parse query xcode for text and images by the user app

Hello



I'm trying to create my first app using xcode.


This fails I on the parse query for text and images.


How should I write the query, that the images and text from the user registered in the app is displayed.


That I cobbled together and work not just as planned.


override func viewDidLoad() {
        super.viewDidLoad()
        let query = PFQuery(className: "Post")
        query.findObjectsInBackground(block: { (object, error) in
            if error != nil{
                print(error)
            }else{
                if let posts = object {
                    for post in posts {
                    query.whereKey("imageFile", equalTo: self.imageFile)
                    query.whereKey("message", equalTo: self.comments)
                    query.getObjectInBackground(withId: "post")
                     
                        self.comments.append(post["message"] as! String)
                        self.imageFile.append(post["imageFile"] as! PFFile)
                        self.tableView.reloadData()
                }
            }
         
                }
         
    })
}



Thank you for your help.



Best regards Daniel

Yes, that is the same


I tried this now so. And the output is correct, it appears about me by the logged on user

override func viewDidLoad() {
        super.viewDidLoad()


        let query = PFQuery(className: "Post")

        query.whereKey("username", equalTo: PFUser.current()?.username)
        query.findObjectsInBackground(block: { (object, error) in

            if let post = object {
                print(post)
   
            }
    })
    }


the issue


Login hat funktioniert

[<Post: 0x6040002bac40, objectId: OtH7UHh9nm, localId: (null)> {

imageFile = "<PFFile: 0x60000065d580>";

message = text;

userId = VBTOwnVeED;

username = "test@test.ch";

}, <Post: 0x6040002adda0, objectId: AoN9iybL7b, localId: (null)> {

imageFile = "<PFFile: 0x60000065ae50>";

message = "Text Diana";

userId = VBTOwnVeED;

username = "test@test.ch";

}, <Post: 0x6000002b4940, objectId: jL1wOLhzAj, localId: (null)> {

imageFile = "<PFFile: 0x600000845f70>";

message = "Einwenig text";

userId = VBTOwnVeED;

username = "test@test.ch";

}, <Post: 0x6000002b8de0, objectId: uTL8P8qVF3, localId: (null)> {

imageFile = "<PFFile: 0x60000065d0d0>";

message = text;

userId = VBTOwnVeED;

username = "test@test.ch";

}, <Post: 0x6000002b8f00, objectId: JVLmJr1kYs, localId: (null)> {

imageFile = "<PFFile: 0x60000065f410>";

message = jdfnsdkfj;

userId = VBTOwnVeED;

username = "test@test.ch";

}, <Post: 0x6000002b8f60, objectId: iAAfck44OR, localId: (null)> {

imageFile = "<PFFile: 0x60000065e8a0>";

message = text;

userId = VBTOwnVeED;

username = "test@test.ch";

}, <Post: 0x6000002b8fc0, objectId: rPpAAPGGmq, localId: (null)> {

imageFile = "<PFFile: 0x60000065f530>";

message = "Test text zum 123";

userId = VBTOwnVeED;

username = "test@test.ch";

}]

whom I now this add

self.comments.append(post["message"] as! String)
                self.imageFile.append(post["imageFile"] as! PFFile)
                self.tableView.reloadData()


in the whole

override func viewDidLoad() {
        super.viewDidLoad()
    
        let query = PFQuery(className: "Post")
    
        query.whereKey("username", equalTo: PFUser.current()?.username)
        query.findObjectsInBackground(block: { (object, error) in
        
            if let post = object {
                print(post)
            
                self.comments.append(post["message"] as! String)
                self.imageFile.append(post["imageFile"] as! PFFile)
                self.tableView.reloadData()
            }
    })
    }


I get this message: Cannot subscript a value of type '[PFObject]' with an index of type 'String'

do you have an idea?

so does it :-)

I had to add line 10

override func viewDidLoad() {
        super.viewDidLoad()

        let query = PFQuery(className: "Post")
   
        query.whereKey("username", equalTo: PFUser.current()?.username)
        query.findObjectsInBackground(block: { (object, error) in
       
            if let posts = object {
                for post in posts{
                print(posts)
           
                self.comments.append(post["message"] as! String)
                self.imageFile.append(post["imageFile"] as! PFFile)
                self.tableView.reloadData()
            }
            }
    })
    }


Only in the TableViwe is not always displayed. It works more times after the log on log off log on but dan's time no longer displays on. Dan, I must again start the Symolater. That should not be, or there can be that the Simulater here?

Yes. And you have corrected yoursel !


object is of type [PFObject]?

Hence

post is of type [PFObject]

so you need probably something like:


           if let post = object {
                print(post)
                for aPost in post {
                     self.comments.append(aPost["message"] as! String)
                     self.imageFile.append(aPost["imageFile"] as! PFFile)
                     self.tableView.reloadData()
               }
            }


Hope this works, it is difficult to check without submitting code to compiler !

I would take the reloadData out of the for post loop


           if let posts = object {
                for post in posts{
                     print(post)     // Ask to print a single post not the whole array
         
                     self.comments.append(post["message"] as! String)
                     self.imageFile.append(post["imageFile"] as! PFFile)
                 } 
                 self.tableView.reloadData()
           }


You may have a thread issue : I suppose the completion is in a specific thread, where UI must be in main thread.


If so, insert reload in


DispatchQueue.main.async {
     self.tableView.reloadData()
}
Accepted Answer

It always works with the

DispatchQueue.main.async {
     self.tableView.reloadData()
}


the final code


  override func viewDidLoad() {
        super.viewDidLoad()
     
        let query = PFQuery(className: "Post")
      
        query.whereKey("username", equalTo: PFUser.current()?.username)
        query.findObjectsInBackground(block: { (object, error) in
          
            if let posts = object {
                for post in posts{
                print(posts)
              
                self.comments.append(post["message"] as! String)
                self.imageFile.append(post["imageFile"] as! PFFile)
                self.tableView.reloadData()
            }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
    })
    }


Thank you for your support much many times. now I can tackle the other issues

Great and good luck.


Don't forget to mark correct answer to close the thread.

Just for you to know next time. Usually, you mark as correct the solution some folk has provided, not your own wrap up.

mark this question as solved quesstion apples ios parse query xcode for text and images by the user app
 
 
Q