Why does my app freeze while I upload data to firebase?
<p><strong>UPDATE:</strong> ****little edit, i found out that actually the only thing that's freezing is retrieving the data from firebase, especially the images that needed to retrieve. the func initOberserver is for retrieving the data from firebase. so this needs to be done every time in background. but the tableview has to be usable in the mean while?****</p>

<p>I'm struggling a bit with background threads. I'm making a firebase app but my app freezes for a while when I upload something to firebase and retrieve it back to the app.</p>

<p>I have 2 constant's in a separate open file:</p>

<pre><code>let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
</code></pre>

<p>I have a viewcontroller: ListVC that retrieves the data from firebase with this function.</p>

<pre><code>func initObservers() {

    //LoadingOverlay.shared.showOverlay(self.view)
    dispatch_async(backgroundQueue, {
        DataService.ds.REF_LISTS.observeEventType(.Value, withBlock: { snapshot in
            print(snapshot.value)

            self.lists = []

            if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {

                for snap in snapshots {
                    print("SNAP: \(snap)")

                    if let listDict = snap.value as? Dictionary&lt;String, AnyObject&gt; {
                        let key = snap.key
                        let list = List(listKey: key, dictionary: listDict)
                        self.lists.insert(list, atIndex:0)
                    }
                }
            }

            //LoadingOverlay.shared.hideOverlayView()
            self.tableView.reloadData()

        })

    })

}
</code></pre>

<p>And then I have a view controller addVC that posts the data to firebase with this function:</p>

<pre><code>@IBAction func postListItem(sender: AnyObject) {

        if let addTitle = addTitle.text where addTitle != "", let addDescription = addDescription.text where addDescription != "", let addLocation = addLocation.text where addLocation != "" {

            dispatch_async(backgroundQueue, {
                self.postToFirebase(nil)

                dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in

                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let listVC = storyboard.instantiateViewControllerWithIdentifier("TBC") as! UITabBarController
                    listVC.selectedIndex = 1
                    self.presentViewController(listVC, animated: false, completion: nil)
                })
            })
        }
    }

func postToFirebase(imgUrl: String?) {

        LoadingOverlay.shared.showOverlay(self.overlayView)

        var post: Dictionary&lt;String, AnyObject&gt; = ["username": currentUsername, "description": addDescription.text!, "title": addTitle.text!, "location": addLocation.text!, "images": self.base64String]

        let firebasePost = DataService.ds.REF_LISTS.childByAutoId()
        firebasePost.setValue(post)

    }
</code></pre>

<p>As you can see I tried it with code and explanations I found on the Internet and as on Stack Overflow. But still if I open my app and go to the listVC it freezes after 2 sec for maybe 10 seconds, and when I post something, it also freezes for a while when it goes to the listVC.</p>

<p>I've also this code:</p>

<pre><code>let uploadImage = image
        let imageData = UIImageJPEGRepresentation(uploadImage, 0.5)
        self.base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
</code></pre>

<p>without this code, it doesn't freezes and it does 'it's thing' in a sec. but i need this code to post images to firebase?</p>