Post

Replies

Boosts

Views

Activity

Reply to Swift - Async calls in loop
Hi OOPer (my hero in this forum :) ) Does the method getDocuments(completion:) calls completion in the main thread? well, let me explain it like this: I trigger the loadReplies.. function once I fetched the initial comment objects from the database. The comment objects are stored in the dataSource which is passed as a parameter. Once loadReplies is called, my view controller doesn't care actually about the replies as long as they are not fully fetched and returned back via delegate pattern. From my understanding, the fetches above are running NOT in the main thread or :)
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Swift - Async calls in loop
Hi @OOPer, thanks for the reply. My fetchUserById method looks like this public func fetchUserById(completionHandler:@escaping(_ user: User)->(), uid:String?) { // throws{     var userObject = User()     let _userId = UserUtil.validateUserId(userId: uid)     USER_COLLECTION?.whereField("uid", isEqualTo: _userId).getDocuments(completion: { (querySnapshot, error) in               if error != nil {         print(error?.localizedDescription as Any)       } else {         for document in querySnapshot!.documents {           userObject = User(snapShot: document)           completionHandler(userObject)         }       }     })   }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Swift - Async calls in loop
Hi @Claude31, well, you can tell me if it is a good idea :) I am open for every good solution. Just to give you more background information. To a given comment object, I load all the replies. Those replies are stored in the commentReplyArray array. For each comment reply, I have to execute the async tasks to fetch the information, put it to result array and return it. Yes, the replies should be displayed in a table view to a given comment. I want to add additional cells below the cell with the original comment. Maybe I can share the whole function header as well: func loadReplies(commentItem: CommentItem, isSearchBarFocused:Bool, filteredComments:[CommentItem], dataSource:[CommentItem]) {     var dataSource = dataSource     // Get index of the comment     let index:Int     if (isSearchBarFocused) {       index = filteredComments.firstIndex(of: commentItem)!     }     else {       index = dataSource.firstIndex(of: commentItem)!     }           var ips: [IndexPath] = []                   var results: [Int: CommentItem] = [:]           self.dataAccessService.fetchRepliesByCommentId(completionHandler: { (commentReplyArray) in       let group = DispatchGroup()       for var i in 0..<commentReplyArray.count {         let commentReplyObject = commentReplyArray[i]         let commentItem = CommentItem()                   group.enter()           self.dataAccessService.fetchUserById(completionHandler: { (userObject) in             commentItem.userObject = userObject             results[i] = commentItem             defer { group.leave() }          }, uid: commentReplyObject.userId)                           group.enter()           self.dataAccessService.fetchDownloadURLOfProfileImage(organizerId: commentReplyObject.userId) { (contentURL) in           commentItem.userObject.contentURL = contentURL            defer { group.leave() }          }                   group.notify(queue: .main) {           commentItem.commentObject = commentReplyObject           let array = commentReplyArray.compactMap { _ in results[i] }           print(array.count)         }                }     }, commentId: commentItem.commentObject.id)   } This time, I tried to move the  let group = DispatchGroup() into the loop
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20