没有在viewDidLoad中更新标签?

时间:2016-05-31 11:27:47

标签: ios swift asynchronous delegates

我正在创建一个处理Salesforce SDK的iOS应用程序。

要发送请求(此处我发送3个请求),我在viewDidLoad()中编写了代码。

我在

中收到回复
func request(request: SFRestRequest, didLoadResponse jsonResponse: AnyObject)

我试图更新viewDidLoad()中的标签但是没有发生。 请帮帮我?

override func viewDidLoad() {
        super.viewDidLoad()
        let request = SFRestAPI.sharedInstance().requestForQuery("SELECT name,active__c,createdbyid,phone,fax,LastActivityDate,lastmodifiedbyid from account where id='"+tempAccId+"'");//This is my first request to fetch some data
        SFRestAPI.sharedInstance().send(request, delegate: self);
        let requestForCreatedBy = SFRestAPI.sharedInstance().requestForQuery("select name from user where id in (SELECT createdbyId from account where id='\(tempAccId)')");//second request fetch createdby
        SFRestAPI.sharedInstance().send(requestForCreatedBy, delegate: self);
        let requestForlastmodifiedBy = SFRestAPI.sharedInstance().requestForQuery("select name from user where id in (SELECT lastmodifiedbyid from account where id='\(tempAccId)')");//third request to fetch modified by
        SFRestAPI.sharedInstance().send(requestForlastmodifiedBy, delegate: self);
        //To draw a line under the tittle
        let path:UIBezierPath = UIBezierPath()
        path.moveToPoint(CGPoint(x: 0, y: 145))
        path.addLineToPoint(CGPoint(x: self.view.frame.width, y: 145))

        let shapeLAyer:CAShapeLayer = CAShapeLayer()
        shapeLAyer.path = path.CGPath
        shapeLAyer.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).CGColor
        shapeLAyer.lineWidth = 2.0
        self.view.layer.addSublayer(shapeLAyer)
        // Do any additional setup after loading the view.


        self.AccountTittle.text = name
        self.accountName.text = name
        self.isActive.text = active
        self.lastActivityDate.text = lastActiv
        self.phone.text = phonevar
        self.fax.text = faxvar
        self.createdBy.text = createdByName
        print(createdByName)
        self.lastModifiedBy.text = modifiedByname
        print(createdByName)

    }

3 个答案:

答案 0 :(得分:2)

正如许多评论中所解释的那样,在您终止异步请求调用之前,您的UI更新已经发生。所以,你可以修改你的

SFRestAPI.sharedInstance().send(<requestVar>, delegate: self)

有类似的东西:

SFRestAPI.sharedInstance().send(<requestVar>, delegate: self, { (success) -> Void in

    // When request completes,control flow goes here.
    if success {
        // request success
        // update my labels
    } else {
        // request fail
    }
})

所以,你可以像这样转换你的函数:

typealias CompletionHandler = (success:Bool) -> Void

func send(<requestVar>, delegate: self, completionHandler: CompletionHandler) {

    // request code.

    let flag = true // true if request succeed,false otherwise

    completionHandler(success: flag)
}

<强>更新

使用:

SFRestAPI.sharedInstance().sendRESTRequest(request, failBlock: {error in print(error)}, completeBlock: { response in 
     print(response) 
     // update labels
})

编辑:关于您的UI更新:

你创造了更多的shapeLAyer ..

将shapeLAyer声明为类var:

var shapeLAyer:CAShapeLayer!

let path:UIBezierPath = UIBezierPath()
path.moveToPoint(CGPoint(x: 0, y: 145))
path.addLineToPoint(CGPoint(x: self.view.frame.width, y: 145))

self.shaperLAyer?.removeFromSuperlayer() //remove it if it's exist yet
self.shapeLAyer = CAShapeLayer()
shapeLAyer.path = path.CGPath
shapeLAyer.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).CGColor
shapeLAyer.lineWidth = 2.0
self.view.layer.addSublayer(shapeLAyer)

确保您的UI代码在主线程上运行,您可以在

中编写您的UI
dispatch_async(dispatch_get_main_queue(), { 
   // update your UI in the main thread 
})

我不知道函数createdByName和modifiedByname,如果你也发布它们(完整代码)我会尝试帮助它..

答案 1 :(得分:1)

从我的代码中我发现的理解是,您正在启动请求并立即设置带有一些变量的标签文本,这不起作用。我认为你在委托方法中设置变量,这是在看到标签文本后触发的,所以尝试在func dispatch_async(dispatch_get_main_queue()) {方法本身中更新UI

<强>更新 我不熟悉salesforce sdk,我能理解的是,如果我们使用委托方法,我们无法区分该方法调用哪个请求,因此如果我们创建并调用多个,那么最好使用基于块的函数同一级别的请求。 为此你可以使用

request(request: SFRestRequest, didLoadResponse jsonResponse: AnyObject)

正如GRiMe2D所指出的那样,该块可能正在任何其他线程中执行,以便更新UI中的用户界面

let request = SFRestAPI.sharedInstance().requestForQuery("SELECT name,active__c,createdbyid,phone,fax,LastActivityDate,lastmodifiedbyid from account where id='"+tempAccId+"'");//This is my first request to fetch some data
SFRestAPI.sharedInstance().sendRESTRequest(request, failBlock: { error in
            //handle your error
            }) { json in
             //handle your response for SELECT name,......
        }

答案 2 :(得分:0)

我不确定Salesforce的工作原理,但一般来说,如果是异步调用,请等待响应并在此之后设置标签的文本。在viewDidload中放置断点并查看设置时的文本内容。

如果是异步通话,您可能正在通过代表或通过块进行某种回叫,请在收到回拨后设置标签的文本。希望它有所帮助。