Singleton with Swift 3.0

Singleton with properties in Swift 3

For me this is the best way, make init private. Swift 3 \ 4 \ 5 syntax

// MARK: - Singleton

final class Singleton {

// Can't init is singleton
private init() { }

// MARK: Shared Instance

static let shared = Singleton()

// MARK: Local Variable

var emptyStringArray = [String]()

}

Create singleton of a viewcontroller in swift 3

If you really wanted to have singleton for a view controller corresponding to some scene, you'd probably do something like:

class SecondViewController: UIViewController {

static let shared = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Foo")

}

In this example, the storyboard was Main.storyboard and the storyboard identifier for the scene in question was Foo. Obviously, replace those values for whatever was appropriate in your case.

Then your other view controller that was invoking this could do something like:

@IBAction func didTapButton(_ sender: Any) {
let controller = SecondViewController.shared
show(controller, sender: self)
}

I wouldn't recommend singletons for view controllers. View controllers (and their views) should be created when needed and be allowed to be deallocated when they're dismissed. And you're losing many storyboard benefits (by which you see the logical flow between scenes with segues between them). And, if you use this view controller in different contexts, you're inviting problems stemming from the view controller hierarchy falling out of sync with the view hierarchy. I really would discourage you from using singletons for view controllers.

But if you were going to do it, you could do something like that...

Swift 3, singleton pattern and having separate classes

In general, you should regard view controllers as not reusable, because they mediate between this particular data and this particular interface (model-view).

But if you can isolate this particular functionality into an independent object, that would make it reusable. What I usually do is make a custom struct and have the view controller own this as a helper object. I don't quite see the point of the putting the struct declaration in a separate file, since it's easy to copy and paste, but you can certainly do that if you like.

(Nor do I see what any of this has to do with Singleton.)

Swift 3: Property observer for singleton

It's not possible to have a blanket observer for a single Realm Object at the moment, however there's an issue tracking it in the Realm GitHub and I believe it's being prioritized at the moment.

Alcivanio is correct in that you'll need to manually trigger an update event to ensure the changes are persisted to Realm. That being said, because Realm controls the accessors in persisted objects, didSet isn't guaranteed to be called once an object has been saved to Realm. For simplicity reasons, I'd recommend using a closure to update the user object.

Updated Answer: If your singleton is a Realm Object, you can't modify its properties outside of a write transaction. Like I wrote below, the easiest thing to do would be to be to have an update closure that managed the Realm write transaction, and you simply update the properties of your Singleton object inside it.

As you've also seen, you can't use didSet with a property defined with let, so it might be best to drop back to Objective-C conventions and use an internal instance variable.

So all up, maybe consider doing this:

fileprivate var _current: User? = nil

class User: Object {
dynamic var name:String = ""
dynamic var email:String = ""
dynamic var id:String = ""
dynamic var picURL:String = ""
dynamic var pic:Data = Data()

static var current: User
{
if _current == nil {
let realm = try! Realm()

// Query for an existing user
_current = realm.objects(User.self).first

// Create a new user if it didn't exist in Realm
if _current == nil {
_current = User()
try! realm.write { realm.add(_current) }
}
}
return _current
}

public func update(_ block: (() -> Void)) {
let realm = try! Realm()
try! realm.write(block)
}
}

let user = User.current
user.update {
user.name = "DCDC"
}


Related Topics



Leave a reply



Submit