evermeer / EVReflection

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift
Other
965 stars 119 forks source link

how to use first screen response in another class (view controller)? #282

Closed saikishoreagrapalem closed 6 years ago

saikishoreagrapalem commented 6 years ago

// Network calling for first screen and getting response and setting in to the model.

RTWebService.sharedInstance.webServiceInitialGETCall(url: "/api/CurrentLocationandNews?ostype=ios&latitude=(curentLat)&longitude=(curentLong)&secretcode=$3cr3t&appv=1.0.0", paramValues: [

        :] as [String : Any] as NSDictionary,  completionBlock: {
            (error , response ) -> Void in

            if let responseObj = response
            {
                print(responseObj)

                if (responseObj["status"] as! String == "success")
                {
                    let model = CommonObjects(dictionary: response! as NSDictionary) as CommonObjects

                    print(model)

                    let notifyModel = model.notifications_list![0]
                    self.lblScrollingNotification.text = String.NonNilString(str: notifyModel.messagetext)
                    self.lblScrollingNotification.text = notifyModel.messagetext

                    self.lblScrollingNotification.frame.origin.x = ScreenSize.SCREEN_WIDTH

                    self.lblScrollingNotification.sizeToFit()
                    UIView.animate(withDuration: 12.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in

                        self.lblScrollingNotification.center = CGPoint(x: -(self.lblScrollingNotification.frame.size.width / 2), y : self.lblScrollingNotification.center.y)

                    }, completion:  { _ in })

                    self.weatherApiCall()

                }

                else
                {

                }
            }
            else
            {
                if error != nil {
                    if !(error?.localizedDescription.isEqual("cancelled"))!
                    {
                        print("Error :\((error?.localizedDescription)!)")

                    }
                }
            }

    });

// Model

class RTModel: NSObject {

}

class CommonObjects: EVObject {

var status = String()
var notifications_list: [Transaction]?

override public func propertyMapping() -> [(keyInObject: String?, keyInResource: String?)] {
    return [(keyInObject: "ignoredProperty",keyInResource: nil), (keyInObject: "propertyInObject",keyInResource: "keyInJson")]
}

}

public class Transaction: EVObject {

var messagetext: String?

override public func propertyMapping() -> [(keyInObject: String?, keyInResource: String?)] {
    return [(keyInObject: "ignoredProperty",keyInResource: nil), (keyInObject: "propertyInObject",keyInResource: "keyInJson")]
}

}

// now I want to use this same response in the another view controller class file. please help me for this same ?? @evermeer

evermeer commented 6 years ago

There are various ways to handle async code. When you search for articles you can find a lot about it. See for instance: https://medium.com/ios-os-x-development/managing-async-code-in-swift-d7be44cae89f and https://medium.com/@zhxnlai/async-programming-in-swift-with-asynctask-95a708c1c3c0

In your case the question is if you already have a reference somewhere to your view controller. If so, then just add a public property to that view controller and assign the data to it. Maybe through a function so that you could change the UI depending on the data. If you don't have a reference to the viewcontroller or if it's uncertain if the reference is already available, then you need to save the data somewhere so that the viewconroller can access it (on present or so).

Usually when data from a network call is needed in multiple view controllers you will execute that call outside the view controller. For instance from a network layer where you could also subscribe to changes of the returned data. Then both your initial screen and your other view controller could subscribe to the data and you will start the data fetch from the first.

one other thing. The code below your 'print(model)' access the UI. I assume that your network call is executed on a background thread. You should execute UI changes on the main thread. You can use DispatchQueue.main.async { .. } for that.