Ramcharan Reddy Ramcharan Reddy - 6 months ago 23
Swift Question

Updating of labels in viewDidLoad is not happening?

I'm creating an iOS application that is dealing with Salesforce SDK.

To send requests(here I'm sending 3 requests), I wrote the code in

viewDidLoad()
.

I'm getting the response in

func request(request: SFRestRequest, didLoadResponse jsonResponse: AnyObject)


I'm trying to update the labels in
viewDidLoad()
but it is not happening.
Please help me?

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)

}

Answer

As explained in many comments, your UI updates come before you have terminated your asynchronous request calls. So, you can modify your

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

with something like:

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
    }
})

So , you can transform your function like this:

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)
}

UPDATE:

Use:

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

EDIT: About your UI updates:

You create too more shapeLAyer..

declare your shapeLAyer as a class 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)

Be sure your UI code running on the main thread, you can write your UI inside the

dispatch_async(dispatch_get_main_queue(), { 
   // update your UI in the main thread 
})

I don't know the functions createdByName and modifiedByname, if you post also them (full code) i'll try to help with it..

Comments