首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >popToViewController先执行

popToViewController先执行
EN

Stack Overflow用户
提问于 2014-12-30 03:24:41
回答 2查看 293关注 0票数 0

当按下post按钮时,下面的函数将执行。在函数中,使用Parse后端检索的所有对象都附加到groupConversation数组,这是一个全局数组。但是,当我引用UITableViewController中弹出的接近函数末尾的数组并使用println()打印数组的内容时,数组是空的。但是,当我在包含此函数的println()中使用UIViewController时,数组显示为包含一个对象。在控制台中,一旦按下按钮,就会弹出UITableViewControllerprintln(),在包含以下函数的UIViewControllerprintln()之前执行。如何使下面的函子在弹出到UITableViewController之前完全执行。

代码语言:javascript
复制
@IBAction func postButtonPressed(sender: AnyObject) {
    //Adds Object To Key
    var name=PFObject(className:currentScreen)
    name["userPost"] = textView.text
    name.saveInBackgroundWithBlock {
        (success: Bool!, error: NSError!) -> Void in

        if success == true {

            self.textView.text=""


        } else {

            println("TypeMessageViewController Error")

        }


    }
    //Gets all objects of the key
    var messageDisplay = PFQuery(className:currentScreen)
    messageDisplay.selectKeys(["userPost"])
    messageDisplay.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil{
            for object in objects {
                var textObject = object["userPost"] as String
                groupConversation.append(textObject)

                }
        } else {
            // Log details of the failure

        }
        println("Type message \(groupConversation)")
    }
    navigationController!.popToViewController(navigationController!.viewControllers[1] as UIViewController, animated: true)
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-30 03:53:36

问题在这里,messageDisplay.findObjectsInBackgroundWithBlock。当您在后台线程中执行此操作时,它将与主线程分离。您的主线程将按其应有的方式执行。

因此,在完成任务之前,您的主线程会弹出视图。

代码语言:javascript
复制
messageDisplay.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]!, error: NSError!) -> Void in
    if error == nil{
        for object in objects {
            var textObject = object["userPost"] as String
            groupConversation.append(textObject)

            }
    } else {
        // Log details of the failure

    }
    println("Type message \(groupConversation)")
    dispatch_async(dispatch_get_main_queue()) {
        self.navigationController!.popToViewController(navigationController!.viewControllers[1] as UIViewController, animated: true)
        return
    }
}

在后台线程中推和弹出可能会导致问题。因此,在后台执行任务后获取主线程,然后在主线程中弹出。

在快速单语句闭包中,自动返回语句返回值。在您的具体情况下,它试图返回AnyObject的一个实例,该实例是popToViewControllerAnimated的返回值。dispatch_afteris Void -> Void所期望的闭包。因为闭包返回类型不匹配,所以编译器会抱怨这一点。

希望这有帮助..。;)

票数 1
EN

Stack Overflow用户

发布于 2014-12-30 03:39:12

您在异步代码中遇到了一个非常常见的问题。两个...InBackgroundWithBlock {}方法都在后台运行某些内容(异步)。

我发现最好的例子是:

当您启动异步代码块时,就像将鸡蛋放在上面煮沸一样。你还可以包括一些当他们煮完后应该做的事情(块)。这可能就像去掉蛋壳和切鸡蛋一样。

如果你的下一段代码是“黄油面包,把鸡蛋放在面包上”,你可能会得到意想不到的结果。你不知道鸡蛋是否已经煮熟了,或者额外的任务(去掉外壳,切片)是否已经完成。

你必须以一种异步的方式思考:做这个,然后当它完成的时候,做这个,等等。

就您的代码而言,对popToViewController()的调用应该在异步块中进行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27699226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档