Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:25:34+00:00 2026-06-10T20:25:34+00:00

Looking for some advice on stablizing my app. First some requirements – files with

  • 0

Looking for some advice on stablizing my app. First some requirements – files with PII (personally identifying information) must be encrypted when on disk. Tumbnails and logos are in the custom TableViewCells (if available) and must be decrypted before display.

There are several layers of threading going on. There is a central function getFileData that checks to see if files are on the device or if the files need to be obtained from the network. I desire to keep the UI responsive and (I think) therein lies my problem.

Here is some code:
This is the workhorse method for processing files in my application. It decides where the file is decrypts it and hands it back to a callback:

    -(void)fetchFileData:(UserSession *) session
              onComplete: (void(^)(NSData* data)) onComplete 
{
    NSURL *url = [File urlForMail:self.fileId andSession:session];
    //NSLog(@"File id: %@", self.fileId);
    NSString *encryptionKey = session.encryptionKey;
    dispatch_queue_t cryptoQ = dispatch_queue_create(FILE_CRYPTOGRAPHY_QUEUE, NULL);

    dispatch_async(cryptoQ, ^(void){
        // Get the file and d/encrypt it
        NSError *error = nil;
        if ([File fileExistsAtUrl:url] == YES) { 
            NSLog(@"file is on disk.");
            NSData *localEncryptedFile = [File getDataForFile:url];
            NSData *decryptedFile = [RNDecryptor decryptData:localEncryptedFile 
                                                withPassword:encryptionKey 
                                                       error:&error];
            onComplete(decryptedFile);
            dispatch_release(cryptoQ);
        } else { 
            //NSLog(@"File is not on disk");
            NSDictionary *remoteFile = [session.apiFetcher getFileContent:self.fileId 
                                                                 andToken:session.token];
            if (remoteFile && [[remoteFile objectForKey:@"success"] isEqualToString:@"true"]) {                
                NSData *remoteFileData = [remoteFile objectForKey:@"data"];
                NSString *mimeType = [remoteFile objectForKey:@"mimeType"];
                self.mimeType = mimeType;
                NSData *encryptedData = [RNEncryptor encryptData:remoteFileData 
                                                    withSettings:kRNCryptorAES256Settings 
                                                        password:encryptionKey 
                                                           error:&error];
                [encryptedData writeToURL:url atomically:YES];
                onComplete(remoteFileData);
                dispatch_release(cryptoQ);
            }
        }
    });

Here is an example of a getFileData caller:

    +(void)loadThumbnailForMail: (NSNumber*)thumbnailId 
                    session: (UserSession*)session 
                   callback: (void(^)(NSData* data))callback
{
    File *file = [File findFile:thumbnailId inContext:session.mailDatabase.managedObjectContext];    
    dispatch_queue_t fetchQ = dispatch_queue_create(FILE_FETCHER_QUEUE_LABEL, NULL);
    dispatch_async(fetchQ, ^(void) { 
        if (file) {
            [file fetchFileData:session onComplete:^(NSData *data) {
                if (data && file.mimeType) {
                    callback(data);
                }
            }];
        }        
    });    
    dispatch_release(fetchQ);    
}

Here is an example of the TableViewCell that is calling loadThumbnailForMail:

    -(void)loadAndShowThumbnailImage:(Mail*) mail
{
    UIImage *placeHolder = [UIImage imageNamed:@"thumbnail_placeholder.png"];
    [self.thumbnailImageForMail setImage:placeHolder];
    dispatch_queue_t loaderQ = dispatch_queue_create(THUMBNAIL_FETCHER, NULL);
    dispatch_async(loaderQ, ^ {
        [File loadThumbnailForMail: mail.thumbnailId 
                           session: [UserSession instance] 
                          callback: ^(NSData *data) {
                              dispatch_async(dispatch_get_main_queue(), ^{
                                  UIImage *thumbnailImage = [UIImage imageWithData:data];
                                  [self.thumbnailImageForMail setImage:thumbnailImage];
                              });
                      }];
    });
    dispatch_release(loaderQ);
}

I think that my issue here is the callback in my loadThumbnailImage. If the user scrolls fast enough I suspect that there could be two threads trying to access the same TableViewCell

(MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];)

It doesn’t always happen right away but eventually, after some scrolling the tableView list of cells the app crashes with this: * Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘* Collection <__NSCFSet: 0xde6a650> was mutated while being enumerated.’

I need to have the decrypted images in the cells and the first solution (above) does this for me when the images are available but causes the app to crash. I am wondering if some sort of in memory cache would help improve this if I put images in memory there when they were decrypted and checked that cache in loadAndShowThumbnailImage before I kick off all the threads to get and decrypt them.

Thoughts? I have been banging on this for a week now trying different things and would appreciate some fresh perspective.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:25:36+00:00Added an answer on June 10, 2026 at 8:25 pm

    Based on Justins link and subsequent research I ended up going in this direction: In a UITableView, best method to cancel GCD operations for cells that have gone off screen?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for some advice on the best way to retrieve information from
I'm looking for some advice on how to optimise the following process: App reads
I'm looking for some advice on best practice when writing a web app that
Just looking for some advice on how to structure my relationships in an app
I'm looking for some advice on how best to get the first record when
Looking for some advice on the best way to implement localization along with client
I am looking for some advice in regards to getting a very quick display
I'm looking for some advice on the best way to provide a single place
I'm looking for some advice on whether or not I should use a separate
I'm looking for some advice on how to go about reading the online documentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.