I am trying to load photos in Core Data but this requires some background threads, so it happens that my Object gets saved in Core Data without its photo, I get that photo is nil. And the same is happening with the photos of my groups.
In short, at each iteration I save an object in Core Data and the saving happens faster than the loading of my Photo data.
I tried Semaphors, barriers, groups,... but none worked. I am certainly doing something wrong but I don't know what. If someone could help me I would really appreciate, it has been 2 weeks I am struggling with this same problem. This is my code in a simplified version, the full version is accessible below:
// MARK - ViewDidLoad
override func viewDidLoad() {
// This method is only called once, so you want to create any controls or arrays here
super.viewDidLoad()
// Verifying connectivity to internet
if Reachability.isConnectedToNetwork() == true {
// Getting data from Parse + getDataInBackgroundWithBlock
}else{
}
}
// MARK - Action of the login button
#IBAction func loginBtn_click(sender: AnyObject) {
if usernameTxt.text == "" || passwordTxt.text == "" {
self.displayAlert("Error", message: "Please enter a username and password")
}else{
// MARK - Login the user
PFUser.logInWithUsernameInBackground(usernameTxt.text!, password: passwordTxt.text!) { (user, error) -> Void in
if error == nil {
let queue = dispatch_queue_create("com.karagan.app.queue", DISPATCH_QUEUE_CONCURRENT)
dispatch_async(queue, { () -> Void in
self.getObjectsFromQueryWithAscendingOrder("Add", key: "user", value: userName, ascendingOrder: "added", completionHandler: { (objects1) -> Void in
// Update of an array in background
})
})
dispatch_async(queue, { () -> Void in
// Update of an array in background
})
dispatch_async(queue, { () -> Void in
self.getObjectsFromQueryWithAscendingOrder("Group", key: "membersUsername", value: userName, ascendingOrder: "membersUsername", completionHandler: { (objects3) -> Void in
// Update of an array in background
// MARK - Adding all the users in Core data
for var i = 0; i < usersFromParseDataBase.count; i++ {
let entity = NSEntityDescription.entityForName("Users", inManagedObjectContext: self.managedObjectContext)
let newUser = Users(entity: entity!, insertIntoManagedObjectContext: self.managedObjectContext)
if self.arrayAddedUsers.contains(usersFromParseDataBase[i].username){
// Saving new records in Core Data
}
}
})
})
dispatch_async(queue, { () -> Void in
self.getObjectsFromQueryWithDescendingOrder("Group", key: "membersUsername", value: userName, descendingOrder: "conversationUpdate", completionHandler: { (objects4) -> Void in
if array.count == 2 {
// Getting data in background - photo.getDataInbackgroundWithBlock
}
}else{
// Getting data in background - photo.getDataInbackgroundWithBlock
}
}
})
})
dispatch_barrier_async(queue, { () -> Void in
// Doing some stuff in background which needs the data from threads above
})
}else{
// Print error from loginWithUsernaemInBackground
}
}
}
}
This is what gets displayed to the log, so you can clearly see that the barrier gets executed before the loading of pictures is done executing.
TEST 3 - interacting user has been saved to core data
Loading profile image
Loading profile image
Loading group image
Loading profile image
Loading profile image
9
5
Loading group image
Loading group image
Loading group image
Loading group image
The thing is that I need this data for other operations later. My code is not consistent.
I think you are on the right track. When I create a simple program and use dispatch_barrier_async it works perfectly:
let queue = dispatch_queue_create("com.karagan.app.queue", DISPATCH_QUEUE_CONCURRENT)
dispatch_async(queue, { () -> Void in
sleep(1)
print("1")
})
dispatch_async(queue, { () -> Void in
sleep(2)
print("2")
})
dispatch_async(queue, { () -> Void in
sleep(3)
print("3")
})
dispatch_async(queue, { () -> Void in
sleep(4)
print("4")
})
dispatch_barrier_async(queue, { () -> Void in
print("done")
})
Output:
1
2
3
4
done
I think the problem is that dispatch_async doesn't wait for the call toPFFile.getDataInBackgroundWithBlock to finish:
photoFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error == nil {
self.arrayImageFiles.append(imageData!)
print("Loading group image")
}
})
Try switching all calls to getDataInBackgroundWithBlock with the synchronous call to PFFile.getData (https://parse.com/docs/osx/api/Categories/PFFile%28Synchronous%29.html#/c:objc%28cs%29PFFile%28im%29getData:) instead:
var imageData = photoFile.getData
if (imageData != nil) {
self.arrayImageFiles.append(imageData!)
}
This shouldn't really have any performance issues since you are already executing this code asynchronously.
Related
I have a function for getting user session. I successfully get the session. But inside the DispatchQueue i lose task object (task:AWSTask< AWSCognitoIdentityUserSession >).
Before DispatchQueue it is not null but in the dispatch DispatchQueue it is null. Some how i am losing the reference.
What is the best way to get and object reference from outside of the DispatchQueue. (I dont want to create a general variable in the class.)
BTW this is not happening all the time.
var pool:AWSCognitoIdentityUserPool
override init(){
pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
super.init()
}
func getUserPool() -> AWSCognitoIdentityUserPool {
return pool
}
func getUserSession(completition: #escaping () -> Void)
{
let user = pool.currentUser()!
let task = user.getSession()
task.continueWith{ (task:AWSTask<AWSCognitoIdentityUserSession>) in
{
DispatchQueue.main.async
{
if(task.result != nil && task.error == nil)
{
/*
There are some calculations here
*/
completition()
}
}
}
}
}
I am a new developer. I'm using Swift 4.2 and Xcode 10.2.
I'm trying to show a spinner while a photo is being saved. I am simulating a slow connection on my iPhone in Developer Mode to test it. Using the code below the spinner does not show up. The view goes right to the end where the button shows and says the upload is complete (when it isn't). I tried putting it all into a DispatchQueue.global(qos: .userinitiated).async and then showing the button back on the main queue. I also tried putting the showSpinner on a DispatchQueue.main and then savePhoto on a .global(qos: .utility). But I cleary don't understand the GCD processes.
Here's my code:
func savePhoto(image:UIImage) {
// Add a spinner (from the Extensions)
self.showSpinner(onView: self.view)
PhotoService.savePhoto(image: image) { (pct) in
// Can show the loading bar here.
}
// Stop the spinner
self.removeSpinner()
// Show the button.
self.goToPhotosButtonLabel.alpha = 1
self.doneLabel.alpha = 1
}
What types of DispatchQueues should I use and where should I put them?
Here is the savePhoto code:
static func savePhoto(image:UIImage, progressUpdate: #escaping (Double) -> Void) {
// Get data representation of the image
let photoData = image.jpegData(compressionQuality:0.1)
guard photoData != nil else {
print("Couldn't turn the image into data")
return
}
// Get a storage reference
let userid = LocalStorageService.loadCurrentUser()?.userId
let filename = UUID().uuidString
let ref = Storage.storage().reference().child("images/\(String(describing: userid))/\(filename).jpg")
// Upload the photo
let uploadTask = ref.putData(photoData!, metadata: nil) { (metadata, error) in
if error != nil {
// An error during upload occurred
print("There was an error during upload")
}
else {
// Upload was successful, now create a database entry
self.createPhotoDatabaseEntry(ref: ref, filename: filename)
}
}
uploadTask.observe(.progress) { (snapshot) in
let percentage:Double = Double(snapshot.progress!.completedUnitCount /
snapshot.progress!.totalUnitCount) * 100.00
progressUpdate(percentage)
}
}
Since the code that saves the photo is asynchronous , so your current code removes the spinner directly after it's added before the upload is complete
func savePhoto(image:UIImage) {
// Add a spinner (from the Extensions)
self.showSpinner(onView: self.view)
PhotoService.savePhoto(image: image) { (pct) in
// remove spinner when progress is 100.0 = upload complete .
if pct == 100.0 {
// Stop the spinner
self.removeSpinner()
// Show the button.
self.goToPhotosButtonLabel.alpha = 1
self.doneLabel.alpha = 1
}
}
}
Here you don't have to use GCD as firebase upload runs in another background thread so it won't block the main thread
I want to run some tasks which are dependent on each other so should be performed in an order. Currently, it blocks my UI thread and also there is some issue in ordering.
Couple of questions regarding this:
Tasks are not performed in correct order. What change to be made if we want them to be performed one after other
Is the code optimised in terms of memory usage and resource consumption? How it can be made more optimised?
Do we need global queues inside function call also as shown in code below?
Here are my code details. I have created some serial queues as follows:
var Q0_sendDisplayName=dispatch_queue_create("Q0_sendDisplayName",DISPATCH_QUEUE_SERIAL)
var Q1_fetchFromDevice=dispatch_queue_create("fetchFromDevice",DISPATCH_QUEUE_SERIAL)
var Q2_sendPhonesToServer=dispatch_queue_create("sendPhonesToServer",DISPATCH_QUEUE_SERIAL)
I have an idea that serial queues perform tasks in order so i have called my tasks on serial queues. Here is my code:
dispatch_sync(Q0_sendDisplayName,
{
self.sendNameToServer(displayName){ (result) -> () in
dispatch_sync(self.Q1_fetchFromDevice,
{
self.SyncfetchContacts({ (result) -> () in
dispatch_sync(self.Q2_sendPhonesToServer,
{ self.SyncSendPhoneNumbersToServer(self.syncPhonesList, completion: { (result) in
//.......
//....
The code inside these functions is also running on global queue. Dont know if it is a correct way to code. I have used completion handlers to notify that method has completed executing. Here is the code of function1:
func sendNameToServer(var displayName:String,completion:(result:Bool)->())
{
Alamofire.request(.POST,"\(urlToSendDisplayName)",headers:header,parameters:["display_name":displayName]).responseJSON{
response in
switch response.result {
case .Success:
return completion(result: true) //......
Here is the code of function2. This function is long as it reads whole contacts book so i have placed it inside global queue(dont know if it is right way). I call completion handler on main queue. Here is code:
func SyncfetchContacts(completion:(result:Bool)->())
{
let contactStore = CNContactStore()
var keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataAvailableKey,CNContactThumbnailImageDataKey, CNContactImageDataKey]
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)){
do {
try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: keys)) { (contact, pointer) -> Void in
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
for phoneNumber:CNLabeledValue in contact.phoneNumbers {
let a = phoneNumber.value as! CNPhoneNumber
}
}
}
dispatch_async(dispatch_get_main_queue())
{
completion(result: true)
}
}
//........
Here is the code for function3 which again inside has a global queue(dont know if its right) and calls completion handler on main queue.
func SyncSendPhoneNumbersToServer(phones:[String],completion: (result:Bool)->()){
Alamofire.request(.POST,"\(url)",headers:header,parameters:["display_name":displayName]).responseJSON{
response in
switch response.result {
case .Success:
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0))
{
//enter some large data in database in a loop
dispatch_async(dispatch_get_main_queue())
{
return completion(result: true)
}
}//......
In SyncfetchContacts you are calling the completion handler before contactStore.enumerateContactsWithFetchRequest has finished, outside of its completion closure.
Simply move it in there:
func SyncfetchContacts(completion:(result:Bool)->()) {
...
do {
try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: keys)) { (contact, pointer) -> Void in
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
for phoneNumber:CNLabeledValue in contact.phoneNumbers {
let a = phoneNumber.value as! CNPhoneNumber
}
}
// here ...
dispatch_async(dispatch_get_main_queue()) {
completion(result: true)
}
}
// ... not here.
// dispatch_async(dispatch_get_main_queue()) {
// completion(result: true)
// }
}
}
I'm having some trouble writing a process to allow a user to delete their Firebase account which deletes their account and all the posts that they had in their account including images. I am querying the Users posts in Firebase Database, looping through and deleting them as they come out. In the middle of this loop, I call a function passing in the image URL to delete the image from Firebase storage. The problem I am having is that I can't get an ordered result because of asynchronous stuff. I thought having completion handlers would give it some order. Should I be looking into dispatch groups? Any help is greatly appreciated.
The start of my process with function call:
ref.deleteFirebaseDBUsersPosts(deleter) { (success) -> Void in
if success {
print("The user and their info is completely gone!")
}
}
Function that queries Firebase and loops through users posts and deletes:
func deleteFirebaseDBUsersPosts(uid: String, completion:(success: Bool) -> Void) {
let usersCurrentId = uid
ref.child(“posts”).queryOrderedByChild("uid").queryEqualToValue("\(uid)").observeEventType(.Value, withBlock: {snapshot in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot{
print("SNAP: \(snap)")
if let postedImg = snap.childSnapshotForPath("imageUrl").value {
let postImgUrl = postedImg as! String
self.deletingOldUsersImgsFromStorage(postImgUrl) { (success) -> Void in
}
}
let key = snap.key
ref.child(“posts”).child(key).removeValue()
ref.child(“users”).child(usersCurrentId).removeValue()
}
print("COMPLETING")
completion(success: true)
}
})
}
Deleting the image from Firebase Storage:
func deletingOldUsersImgsFromStorage(postImgUrl: String!, completion:(success: Bool) -> Void) {
let deleteImgRef = ref.child(“images”).child("[imageNAME]")
deleteImgRef.deleteWithCompletion({ (error) in
if (error != nil) {
print("DEVELOPER: There was an error when trying to delete the image from Firebase Storage")
} else {
print("IMAGE SUCCESSFULLY DELETED FROM FIREBASE STORAGE")
}
completion(success: true)
})
}
I dealt with a similar challenge when deleting a user and their data from Firebase. I handled it using dispatch groups to delete the user after my methods that deleted their data had completed.
func deleteUsersData(completion: (success: Bool) -> Void) {}
func deleteUsersPhotoUrl(completion: (success: Bool) -> Void) {}
func deleteUserFromFirebase(completion: (success: Bool) -> Void) {}
let group = dispatch_group_create()
dispatch_group_enter(group)
deleteUsersData { (success) in
if success {
dispatch_group_leave(group)
}
}
dispatch_group_enter(group)
deleteUsersPhotoUrl { (success) in
if success {
dispatch_group_leave(group)
}
}
dispatch_group_notify(group, dispatch_get_main_queue()) {
deleteUserFromFirebase({ (success) in
print("Completely deleted user from Firebase")
})
}
Dispatch groups are great for handling situations like this where you only want to perform a task after the other tasks you specified have completed. Here is more info on how to use dispatch groups.
I am trying to make Asynchronous call (to Parse) and during the call I DON'T want my Main UI to get hung.
Below is the function I am trying to call my ViewController. Below that function is the code I am using from my ViewController.
In the line sleep(4) line the main UI of the ViewController LoginVC does get stuck. Am I not using callbacks for the Asynchronous call correctly?
class Utils {
func logIntoWebService(uname: String, pwd: String, completion: ((result:Bool?) -> Void)!) {
PFUser.logInWithUsernameInBackground(uname, password:pwd) {
(user, error) -> Void in
if error == nil {
if user != nil {
// Yes, User Exists
//UI IS STUCK DURING THIS SLEEP(10)
sleep(10)
completion(result: true)
} else {
completion(result: false)
}
} else {
completion(result: false)
}
}
}
}
I am calling the above function from my ViewController. Below is the call from my ViewController
class LoginVC: UIViewController {
var mUtils: Utils = Utils()
mUtils.loggedInStatus({ (result) -> Void in
println("Response from logInToParse = " + String(stringInterpolationSegment: result))
})
}
You are getting confuse between background threads and completion handler.
logInWithUsernameInBackground is a async function that runs in the background however all the code that runs in the completion handler runs back in the main thread:
completion: ((result:Bool?) -> Void)!) {//The code here runs in the main thread}
So basically from the time that your application start communicate with Parse.com, until the result was back, that code was running asynchronous in a background thread, when it finish and your application received the response it runs in the completion block back in the main thread.
Now lets say you really want to run some code in the background in the completion block, than you would use something like:
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
println("Now this code will run in the background thread")
})
All the new quality of service classes are:
QOS_CLASS_USER_INTERACTIVE
QOS_CLASS_USER_INITIATED
QOS_CLASS_UTILITY
QOS_CLASS_BACKGROUND
Or in your case:
PFUser.logInWithUsernameInBackground(uname, password:pwd) {
(user, error) -> Void in
if error == nil {
if user != nil {
// Yes, User Exists
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
sleep(10)
})
completion(result: true)
} else {
completion(result: false)
}
} else {
completion(result: false)
}
}
For more information see Apple documentation