使用Completion Handler在重新加载之前等待在线数据

时间:2015-04-09 17:29:13

标签: ios swift asynchronous completionhandler

我从在线数据库加载Core Data,然后从核心数据填充我的tableview(以便它也可以离线使用)。一切都运行正常,除了我无法弄清楚如何使完成处理程序工作,以便在加载之前完成提取。我关注this example,但无法弄清楚它没有调用我的函数(CloudUpdate)。

func CloudUpdate(completionHandler: () -> Void) {
            var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
            var context:NSManagedObjectContext = appDel.managedObjectContext!

            // Clear the table before reloading to avoid duplicates

            let request = NSFetchRequest(entityName: "News")

            let managedObjectContext: NSManagedObjectContext? = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext
            var fetchedResultsController: NSFetchedResultsController?
            var results:NSArray = context.executeFetchRequest(request, error: nil)!

            var bas: NSManagedObject!

            for bas: AnyObject in results
            {
                context.deleteObject(bas as NSManagedObject)
            }

            context.save(nil)


            let url = NSURL(string: "http://www.appsforteacher.com/news.php")
            let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
                var d = NSString(data: data, encoding: NSUTF8StringEncoding)
                var arr = d!.componentsSeparatedByString("<")
                var dataweneed:NSString = arr[0] as NSString
                if let data = NSJSONSerialization.JSONObjectWithData(dataweneed.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSArray
                {
                    for dd in data{

                        //time is set to UTC. See if you can fix it.
                        var dateFormatter = NSDateFormatter()
                        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"


                        var courseID : Int = (dd["courseID"]! as String).toInt()!
                        var timeStamp =  dateFormatter.dateFromString(dd["timeStamp"]! as String)
                        var heading : String = dd["heading"]! as String
                        var detail : String = dd["detail"]! as String


                        var newNews = NSEntityDescription.insertNewObjectForEntityForName("News", inManagedObjectContext: context) as NSManagedObject
                        newNews.setValue(heading, forKey: "heading")
                        newNews.setValue(detail, forKey: "detail")
                        newNews.setValue(timeStamp, forKey: "timeStamp")
                        newNews.setValue(courseID, forKey: "courseID")
                        context.save(nil)

                    }
                }
            }

            task.resume()

            completionHandler()
        }

        func application(application: UIApplication!, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!) {
            CloudUpdate() {
                completionHandler(UIBackgroundFetchResult.NewData)
            }
        }

0 个答案:

没有答案