By Yanis


2018-12-06 15:01:48 8 Comments

I need to use a singleton pattern for save a data and use this data for all my app view.

In my singleton pattern, I want to save a data from alamofire call. I want just call one time alamofire and after saver this data in the singleton pattern.

I've tried to call alamofire in the private init function but when I call the object of singleton pattern alamofire is call also.

How I can this please ?

class AccountManager {
static let sharedInstance = AccountManager()
var data = JSON()

private init() {
    Helper().alomofireGet(URL: "https://wat-stg.stardust-hosting.com/dashboard/home") { home in
        self.data = home
    }
}

}

1 comments

@Durdu 2018-12-06 15:22:58

    class AccountManager {    
        static let sharedInstance = AccountManager()
        var data = JSON()

        private init() {
            Helper().alomofireGet(URL: "https://wat-stg.stardust-hosting.com/dashboard/home") { home in
                self.data = home
            }
        }
    }

Your code should work but the response is coming async of your thread and at a later time than when you are calling init.

Therefore if you use something like:

AccountManager.sharedInstance // it will call init here
print(AccountManager.sharedInstance.data) // most probably if the init was just called a few moments ago, the  data is not yet received from the server.

To further understand this behaviour:

  • mark the events with some prints

private init() {
        print("called init")
        Helper().alomofireGet(URL: "https://wat-stg.stardust-hosting.com/dashboard/home") { home in
            print("received data")
            self.data = home
        }
    }

  • or just use some breakpoints

@Yanis 2018-12-06 15:28:28

I use a dispatch group with dispatch notify ?

@Durdu 2018-12-06 15:36:05

There are several ways to communicate between objects in swift. Common ones: delegation, notification, callbaks. Check you this article: medium.com/@gavin9/…

@Durdu 2018-12-06 15:38:10

It seems that you need a simple way to inform another object when this result comes. Dispatch group is something that you may use for another situation. (something along the lines of waiting for multiple async operations to finish before syncing another)

@Durdu 2018-12-07 07:32:47

Was this helpful?

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] How to call Objective-C code from Swift

  • 2014-06-02 20:05:42
  • David Mulder
  • 250329 View
  • 855 Score
  • 14 Answer
  • Tags:   objective-c swift

30 Answered Questions

[SOLVED] Using a dispatch_once singleton model in Swift

1 Answered Questions

[SOLVED] Getting optional String from singleton

1 Answered Questions

[SOLVED] Plist to struct class with codable and static variables

1 Answered Questions

[SOLVED] Alamofire request doesn't return

2 Answered Questions

[SOLVED] Swift singleton used with init(coder:)

  • 2015-07-27 11:55:05
  • Fantattitude
  • 660 View
  • 3 Score
  • 2 Answer
  • Tags:   ios swift singleton

0 Answered Questions

Singleton Class variables returns nil outside initialization function

2 Answered Questions

[SOLVED] How to parse this JSON using SWIFT3

  • 2017-02-10 13:25:32
  • user7261225
  • 168 View
  • 1 Score
  • 2 Answer
  • Tags:   ios json swift swift3

1 Answered Questions

[SOLVED] Delegate not called

0 Answered Questions

Xcode 7.1, Swift 2 UIWebView Load Error

Sponsored Content