Search code examples
iosswiftsdwebimage

Use of singleton for SDWebImage


I am not 100% familiar with the concept of using singleton for additional frameworks. For example, what is the difference between the following two function calls for SDWebImage, when I should use which. Some small examples would be great

SDWebImageManager().saveImageToCache(myImg, forURL: myUrl)
SDWebImageManager.sharedManager().saveImageToCache(myImg, forURL: myUrl)

Solution

    • when I should use which:

    You should always use

     SDWebImageManager.sharedManager().saveImageToCache(myImg, forURL: myUrl)
       <=> let sharedInstance = SDWebImageManager.sharedManager();
               sharedInstance.saveImageToCache(myImg, forURL: myUrl)
    
    • Why: SDWebImageManager.sharedManager() means: call method sharedManager of class SDWebImageManager to get an instance of Class SDWebImageManager,

    then you call saveImageToCache(...) to call method saveImageToCache of that instance.