0

I am trying to download images from a url and then save them in an array of NSData.

I have a Class called Data Manager in which all my data is stored as well as functions for downloading images and getting data from URL.

In the same class I declare a variable called imageData of type [NSData] and let it equal an empty array as follows:

   var imageData: [NSData] = []

here is what my other 2 functions look like:

func getDataFromUrl(url:NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError? ) -> Void)) {
NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
completion(data: data, response: response, error: error)
}.resume()
}

func downloadImage(url: NSURL){
    print("Download Started")
    print("lastPathComponent: " + (url.lastPathComponent ?? ""))
    getDataFromUrl(url) { (data, response, error)  in
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            guard let data = data where error == nil else { return }
            print(response?.suggestedFilename ?? "")
            print("Download Finished")
            self.imageData.append(data)
            print("you have \(self.imageData.count)")
        }
        print("you still do have \(self.imageData.count)")
    }
}

I call these functions in my app Delegate class under the function didFinishLaunchingWithOptions as so

let dataManager = DataManager()
func application(application: UIApplication, didFinishLaunchingWithOptions      launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    dataManager.URLStringArray.removeAll()

  for url in dataManager.objects.imageURLS {
dataManager.URLStringArray.append(url)
  }
    for url in dataManager.URLStringArray {   
    dataManager.downloadImage(NSURL(string: url)!)
    print(url)
    }

return true
}

In my view controller I go to get the data in the image array via following function:

func returnImageData() -> [NSData] {
    print("your image count is \(imageData.count))")
    return imageData
}

but the array is empty! Even though through the whole process I noticed that the array was becoming larger and larger because the print to the logs were showing the array increasing!

Thanks!

7
  • There's self.imageData and self.ImageData in this excerpt, I hope it's not like that in your real code, that could be the issue. Commented Feb 2, 2016 at 9:32
  • In didFinishLaunchingWithOptions if you remove all items from URLStringArray then the following repeat loop will be skipped. Commented Feb 2, 2016 at 9:41
  • the "imageData" and "ImageData" were typos, in my code there are all the same Commented Feb 2, 2016 at 10:52
  • @vadian , the remove all items is called before the loop starts, once the loops starts and the URLS are being appended it is never called. Commented Feb 2, 2016 at 20:55
  • This is confusing. After removing all items from URLStringArray there is nothing to enumerate. Commented Feb 2, 2016 at 20:58

1 Answer 1

1

Since you are using async call to download the image data, at the the time you are printing the count of the imageData the image is not yet downloaded and so the array is not yet populated. Of course this is assuming that you are using the right property names as Eric.D has pointed out.

Sign up to request clarification or add additional context in comments.

4 Comments

The count of the image data is printing to the logs and it is increasing. However, when I go into my view controller and call the method to get the array in view did load, and then try to populate the array but it shows up as an empty array. I have tried so many different ways of getting this data but it always ends up in an empty array!
returnImageData() is in ViewController, Right? Is there a imageData array in the ViewController too. Shouldn't it return dataManager.imageData
the issue was I needed to create a variable in my singleton instance and then append the images to that and retrieve it
If your dataManager is a singleton and no instance of that is created elsewhere then you can use it as I've mentioned in my comment earlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.