Reputation: 81
My app closes itself during syncing process whenever it has to download lots of images.
It works perfectly fine with emulator, but when I run it on device it closes after downloading few images with "terminated due to memory issue" warning in console.
If I reduce the number of images to download, it work fine on device.
Any suggestions?
Upvotes: 3
Views: 3285
Reputation: 5186
In Simulator, it has enough memory to stored data and its using computer's speed. So you didn't get any issue. When you tried to do the same thing into Device, its very limit space.
Image downloading process is the same which take lots of memory and process. There are many chances we are download image every time. You need to stored image into catch memory and second time, you need to retrieve from it.
You can use SDWebImage for this.
Objective-C:
#import <SDWebImage/UIImageView+WebCache.h>
...
[imageView sd_setImageWithURL:[NSURL URLWithString:@"imagepath"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
Swift:
import SDWebImage
imageView.sd_setImage(with: URL(string: "imagepath"), placeholderImage: UIImage(named: "placeholder.png"))
Upvotes: 0
Reputation: 1143
If you're storing the images that you downloaded in memory you will eventually run out of memory.
Perhaps a better approach is to cache each image you download and not hold all of them in memory.
There's a few libraries to help you out with this,
Objective-C: https://github.com/rs/SDWebImage https://github.com/path/FastImageCache Swift:https://github.com/Haneke/HanekeSwift
Upvotes: 0
Reputation: 5530
These might happen because of memory issue. Your simulator is running on mac PC. So app can easily run without memory problem. But device cannot handle this issue due to its memory.
According to your case, your are doing heavy task.
NSURLSessionDownloadTask
"show the debug navigator (left side) and check Memory and Energy Impact"
. Now run the app and do the same. This will help you to identify the app memory and energy used.Instruments
helps you to monitor your app where exactly its crashing."Windows->Devices->Select your device on left side -> View Device Logs"
then select the latest crash logs of your app.Upvotes: 2