Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it required to to use weak reference's within a singleton class?

I came across a tutorial from raywenderlich were the author gave some good tips on handling threading issues in singleton. But when using closures from within the singleton class he is using 'weak' reference cycle. Is it really required so since the class is a singleton, it should have a single instance always right?

    final class PhotoManager {
      private init() {}
      static let shared = PhotoManager()

      private var unsafePhotos: [Photo] = []

    let concurrentPhotoQueue = DispatchQueue(label: "com.jeesson.googlypuff.photoQueue", attributes: .concurrent)
      var photos: [Photo] {
        var photoCopy:[Photo]!    
        concurrentPhotoQueue.sync {
            photoCopy = self.unsafePhotos
        }
        return photoCopy
      }

      func addPhoto(_ photo: Photo) {

// Do we need 'weak self here.. and why?
        concurrentPhotoQueue.async(flags: .barrier) {[weak self] in
            // 1
            guard let self = self else {
                return
            }
            self.unsafePhotos.append(photo)
            DispatchQueue.main.async { [weak self] in
                //self?.postContentAddedNotification()
            }
        }



      }

    }

The tutorial

like image 973
Jeesson_7 Avatar asked Oct 19 '25 00:10

Jeesson_7


2 Answers

In case of DispatchQueue closures don't add any capture list at all, nowhere.

DispatchQueue closures don't cause retain cycles because self doesn't own them.

Basically capture lists inside a singleton object are not needed as the singleton is never going to be deallocated.

like image 115
vadian Avatar answered Oct 20 '25 15:10

vadian


Some singleton's lifecycle is tied to App lifecycle. Some singleton's lifecycle is tied to login/logout.

So what if the singleton is to be deallocated upon logout? I think that's a valid case where using [weak self] can be useful, even for a singleton.

Otherwise just as vadian said, DispatchQueue closures don't cause retain cycles because self doesn't own them. For more see here

like image 36
mfaani Avatar answered Oct 20 '25 15:10

mfaani