SalokinSwift
SalokinSwift

Reputation: 159

Swift Parse - This query has an outstanding network connection error

I am stuck using parse when making several queries on the same viewcontroller, I know they need to be asynchronous and this is the problem but I am not sure how to go about solving this. I have 3 buttons on this page, one to show the user's pods, one to show his followers, and one who he is following. The code is as follows :

   // Query for user's pods

    let podQuery = Pod.query()
    podQuery?.whereKey("createdBy", equalTo: currentUser as Any)
    podQuery?.includeKey("audio")
    podQuery?.findObjectsInBackground(block: { (objects, error) in
        if error != nil {
            print("Error")
        } else if let pods = objects {
            self.pods.removeAll()
            for pod in pods {
                if let pod = pod as? Pod {
                    self.pods.insert(pod, at: 0)
                }
            }
            self.tableview.reloadData()
        }
    })

    //Query for the user's subscribers

    let subscribersQuery = Following.query()
    subscribersQuery?.whereKey("following", equalTo: PFUser.current()?.objectId as Any)
    subscribersQuery?.includeKey("following")
    subscribersQuery?.findObjectsInBackground(block: {(objects, error) in
        if let objects = objects {
            for object in objects {
                self.subscribers.insert(object as! PFUser, at: 0)
            }
        }
    })

    //Query for the users that the user is subscribed to

    let subscribedQuery = Following.query()
    subscribedQuery?.whereKey("follower", equalTo: PFUser.current()?.objectId as Any)
    subscribersQuery?.includeKey("follower")
    subscribedQuery?.findObjectsInBackground(block: { (objects, error) in
        if let objects = objects {
            for object in objects {
                self.subscribed.insert(object as! PFUser, at: 0)
            }
        }
    })

I am getting the error on the 2nd and 3rd queries.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This query has an outstanding network connection. You have to wait until it's done.'

Let me know if the question needs more context/code. Thank you

Upvotes: 0

Views: 244

Answers (1)

Jitendra Kulkarni
Jitendra Kulkarni

Reputation: 833

I think going by the description, you probably meant:

let subscribersQuery = Followers.query()  //Not Following.query()??

In that case, making this change will make the error go away. If you did mean "Following", then this might be the problem:

I encountered this problem and the reason was that I was using the same query object in consequitive parse accesses. Whether or not this is the case for you depends on how PFObject.query() behaves. I did not find the documentation to be clear enough. If it returns a previously cashed query, then:

let subscribersQuery = Following.query()
//....
subscribersQuery?.findObjectsInBackground(block: {(objects, error) in{(
//..
let subscribedQuery = Following.query()
//....
subscribedQuery?.findObjectsInBackground(block: {(objects, error) in{(
//..

Here, you are performing findObjectsInBackground on the same query which will lead to the error. Try this where you explicitly create a new query. This has worked for me:

subscribersQuery = PFQuery(className: Following) //Again, did you mean Followers here?
subscribedQuery = PFQuery(className: Following)

Upvotes: 1

Related Questions