0

Hi i'm trying to store an array filled with objects so that it doesn't disappear if the app is closed completely.

The Problem: if I use core data and convert the array to NSData then it works. But the app freezes while it's processing the array. I've also tried the transformable datatype but i cant't get it to work. And I can't use NSUserdefaults either because it doesn't support images.

Does anyone have an idea how i might solve this. i'm quite a newbie to programming so this might be an entirely wrong approach.

2
  • How are you going to need to access them? All at once, serially in the order they are in the array or individually in no predefined order? Commented Jul 25, 2015 at 20:19
  • the order is crucial for it to work because i did some ugly coding to fill a table view with the array. let Title = ArrayTable[(indexPath.row*3)] as! String let Plot = ArrayTable[(indexPath.row*3)+1] as! String let Poster = ArrayTable[(indexPath.row*3)+2] as! UIImage Commented Jul 25, 2015 at 21:24

2 Answers 2

1

First save the images in individual files with unique file names in an image directory in the Documents directory. Put the unique file names in the array, not the images.

Then depending on your needs either save the individual per image information in Core Data if quick random access is required. Or save the array in a file.

For 1500 strings of ~100 characters each saving in a single file is probably fine, I would start there and only move up to Core Data if there is a performance problem Core Data would resolve it.

As Ken Beck says: "Do the simplest thing that could possibly work." I don't believe that having 750 images in the array would really work if they were of any substantial size.

Do not use NSUserDefaults.

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

6 Comments

For 1500 strings and 750 image names, the fastest way would be if the array was loaded into memory. Loading the array initially will take a little time (probably a very little.) Once loaded into memory, simple array indexing is going to be much faster than hash lookup, firing a fault, and loading the data for each array index from disk.
i'll try that! but don't get me wrong. the images are stored in the array itself because the images are so small. the total array size is maybe 10mb
You need to convert each image, it they are UIImage instances, to NSData because UIImage does not conform to NSCoding. NSUserDefaults is a bad choice because all items are saved in one plist file, your array and anything else that uses NSUserDefaults, it is designed for small things like, ah: user defaults!
I agree with zaph (voted). Keep it simple. Try a simple array first. Don't try to load 750 images into an memory at one time. Even if they are only 20k each, that's ~15 megabytes of memory. Put image names into your array, and load the images as needed.
@DuncanC Thanks for the edit! I'm on-the-road and being a bad speller auto-complete is killing me.
|
0

Core Data is overkill for simply saving an array to disk.

What kind of objects are you saving?

If you already have logic to convert the array to NSData why not just save the file to either your documents directory or caches directory (caches if it can be recreated if purged, documents if it is unique and contains user state info.)

Edit:

zaph raises some good points in his question. How big is this array (number of elements and total data size.) Is it reasonable to load it all into memory?

If you are looking for a random-access way to load one element at a time, then a database might be reasonable.

The specific solution depends on the particulars of your problem, so we need more info.

5 Comments

I use the array to populate a tableview and it contains strings and images. So it's a bit inconvenient to break up the array.
@AndresVlaeminck How many? 10, 100, 1000, more? Also see my answer.
@zaph 1500 strings and 750 images.
Individual string size? 10 characters, 100, 1000, more?
@zaph character count of the strings is around 250

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.