预览任务完成后,swift执行任务

时间:2016-03-08 11:45:06

标签: mysql swift core-data

在我的swift 2应用程序中,我想发一个http-post-request来获取我的mysql数据库的数据。之后我想把这些数据写入我的核心数据。

我有请求和写的代码部分。

如何获得以下结构。

  1. 申请数据
  2. 如果请求数据完整写入核心数据
  3. 这可能是正确的吗?

    这是我的Sync.swift文件中的SyncMYSQL函数。

    class func SyncMYSQL() {
    
            print("START SYNC MYSQL")
    
            UIApplication.sharedApplication().networkActivityIndicatorVisible = true
            let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
            let defaults = NSUserDefaults.standardUserDefaults()
    
    
    
                let request = NSMutableURLRequest(URL: NSURL(string: "https://xxx")!)
                request.HTTPMethod = "POST"
                let postString = "userid=12"
    
    
                request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
                let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
                    data, response, error in
    
                    // if no internet connection
                    guard error == nil && data != nil else {
                        print("error=\(error)")
                        return
                    }
    
                    let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
    
    
                    let fetchRequest = NSFetchRequest(entityName: "ITEMS")
                    fetchRequest.returnsObjectsAsFaults = false
    
                    do {
                        let results = try managedObjectContext!.executeFetchRequest(fetchRequest)
                        for managedObject in results {
                            let managedObjectData:NSManagedObject = managedObject as! NSManagedObject
                            managedObjectContext!.deleteObject(managedObjectData)
                        }
                    } catch let error as NSError {
                        print("Detele all data in ITEMS error : \(error) \(error.userInfo)")
                    }
    
    
    
                    var x = 0
                    while (x < responseString.count ) {
    
    
                        let newItem = NSEntityDescription.insertNewObjectForEntityForName("ITEMS", inManagedObjectContext: managedObjectContext!) as! CoreData_ITEMS
                        newItem.name = responseString[x]
                        x++
                    }
    
                    dispatch_sync(dispatch_get_main_queue(),{
                        print("FINISH MYSQL")
                        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                    });
    
                }
                task.resume()
            }
    

    我可以通过这种方式从我的视图控制器调用此函数:

    func RequestData() {
    
            Sync.SyncMYSQL()
    print ("Start Core Data")
    
            let fetchRequest = NSFetchRequest(entityName: "ITEMS")
            do {
                try data = managedObjectContext!.executeFetchRequest(fetchRequest) as! [CoreData_ITEMS]
            } catch { }
    
    
    
            Table.reloadData()
        }
    

1 个答案:

答案 0 :(得分:2)

当操作结束时,你应该使用闭包块作为回调

class func SyncMYSQL(onSuccess: ()->()){

    // your custom events
    // your custom events
    // your custom events

    if MYSQLisSynced == true {
        // when MYSQL is synced you can call the success block
        onSuccess()
    }

}

在调用函数SyncMYSQL()时的其他文件中,您必须指定on success block

SyncMYSQL { () -> () in
    //custom callback actions
}