ios - Swift, NSOperationQueue.mainQueue(): update data in operation during operation is running? -
I recently downloaded background file in iOS project, and when the file download starts, updated block by seeing progress Has been started Download is dismissed scene with UI and once present, then First of all you should switch to GCD (Grand Central Dispatch). Your problem arises because the context in Here's a general idea:. NSOperationQueue.mainQueue (). AddOperationWithBlock ()
Method:
func URL session (Session: NSURL session, download task: NSURLSessionDownloadTask, written WrititeData bytes: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) { If totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown {println ("Unknown Transfer Size"); } Else {Data = getDBInfoWithTaskIdentifier (downloadTask.tasIdentifier) NSOperationQueue.mainQueue (). AddOperationWithBlock () {data.db_info.downloadProgress = Float (totalBytesWritten) / Float (totalBytesExpectedToWrite) inf = data.db_info.indexPath let's cell = self-answer tableView.cellForRowAtIndexPath (NSIndexPath (forRow: data.index, insection: 0)) SettingsTableViewCell cell .downloadProgress.hidden = in (data.db_info.downloadProgress as false cell.downloadProgress.setProgress, animated: true)}}}
self.tableView
is a new object, but self.tableView
to NSOperationQueue.mainQueue ()
operation, Which update the progress scene He was the first to reject that which is out of date. Println () has two separate objects lets there self.tableView
that NSOperationQueue.mainQueue ()
likely update data about the blocks? tableView
is captured in closed. You can put the logic in a different class function, so tableview
is not appearing in your closing. This cleaner is an added advantage of a more organized code
// New function function setProgressInCell (data:?) {// '? '(: Data.index, insection: 0) NSIndexPath (forRow) as SettingsTableViewCell cell.downloadProgress.hidden = false cell.downloadProgress because I kind of do not know the inf = data.db_info.indexPath the cell = self.tableView.cellForRowAtIndexPath is. Setprogress (data.db_info.downloadProgress, animated: true)} func URL session (session: NSURL session, download task: NSURLSessionDownloadTask, written by WrititeData bytes: Int64, written totalBytes: Int64, totalBytesExpectedToWrite: Int64) {if totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown { Println ("unknown transfer size"); } Else {enter data = getDBInfoWithTaskIdentifier (downloadTask.tasIdentifier) dispatch_async (dispatch_get_main_queue ()) data.db_info.downloadProgress = Float (totalBytesWritten) / float (totalBytesExpectedToWrite) {[weak self] self.setProgressInCell (data)}}}
Comments
Post a Comment