UPDATE: ****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?****
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.
I have 2 constant's in a separate open file:
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
I have a viewcontroller: ListVC that retrieves the data from firebase with this function.
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<String, AnyObject> {
let key = snap.key
let list = List(listKey: key, dictionary: listDict)
self.lists.insert(list, atIndex:0)
}
}
}
//LoadingOverlay.shared.hideOverlayView()
self.tableView.reloadData()
})
})
}
And then I have a view controller addVC that posts the data to firebase with this function:
@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(), { () -> 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<String, AnyObject> = ["username": currentUsername, "description": addDescription.text!, "title": addTitle.text!, "location": addLocation.text!, "images": self.base64String]
let firebasePost = DataService.ds.REF_LISTS.childByAutoId()
firebasePost.setValue(post)
}
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.
I've also this code:
let uploadImage = image
let imageData = UIImageJPEGRepresentation(uploadImage, 0.5)
self.base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
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?