Search code examples
swiftparse-platformparse-server

Loading data from Parse server into a model file in an array.


I want to load data from parse server into a model file in an array called posts but i don't know how.

Here is the code :

struct Post

{ var createdBy: User var timeAgo: String? var caption: String? var image: UIImage? var numberOfLikes: Int? var numberOfComments: Int? var numberOfShares: Int?

static func fetchPosts() -> [Post]
{
    var posts = [Post]()

    var usernameArray = [String]()
    var avaArray = [PFFile]()
    var dateArray = [Date?]()
    var picArray = [PFFile]()
    var titleArray = [String]()
    var uuidArray = [String]()


    let query = PFQuery(className: "posts")
    query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
        if error == nil {
            usernameArray.removeAll(keepingCapacity: false)
            avaArray.removeAll(keepingCapacity: false)
            dateArray.removeAll(keepingCapacity: false)
            picArray.removeAll(keepingCapacity: false)
            titleArray.removeAll(keepingCapacity: false)
            uuidArray.removeAll(keepingCapacity: false)

            // find related objects
            for object in objects! {



                usernameArray.append(object.object(forKey: "username") as! String)
                avaArray.append(object.object(forKey: "ava") as! PFFile)
                dateArray.append(object.createdAt)
                picArray.append(object.object(forKey: "pic") as! PFFile)
                titleArray.append(object.object(forKey: "title") as! String)
                uuidArray.append(object.object(forKey: "uuid") as! String)
            }

        }
    }

     return posts
}

}

struct User { var username: String? var profileImage: UIImage? }


Solution

  • Try this

    static func fetchPosts() -> [Post]
    {
        var posts = [Post]()
    
        var usernameArray = [String]()
        var avaArray = [PFFile]()
        var dateArray = [Date?]()
        var picArray = [PFFile]()
        var titleArray = [String]()
        var uuidArray = [String]()
    
    
        let query = PFQuery(className: "posts")
        query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
            if error == nil {                
                // find related objects
                if let returnedObjects = objects {
                    for object in returnedObjects {
                        usernameArray.append(object["username"] as! String)
                        avaArray.append(object["ava"] as! PFFile)
                        dateArray.append(object.createdAt)
                        picArray.append(object["pic" as! PFFile)
                        titleArray.append(object["title"] as! String)
                        uuidArray.append(object["uuid"] as! String)
                    }
                }
            }
        }
    
        return posts
    }
    

    In order to append data to your posts you need to - Convert PFFile to UIImage - Create a User object with the UIImage - Add the User object to your posts

    if let avaPicture = object["ava"]! as! PFFile {
        avaPicture.getDataInBackground({ (imageData: Data?, error: Error?) -> Void in
            if (error == nil) {
                let image = UIImage(data: imageData!)
                if image != nil {
                    // Create your User object with UIImage here
                    let user = User()...
    
                    // Add the new user and other data to your `posts`array
                    ...
                }
            }
        })
    }