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

  • SEARCH
  • Home
  • 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 6137855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:46:43+00:00 2026-05-23T17:46:43+00:00

I am looping through an array of file paths retrieved by a core data

  • 0

I am looping through an array of file paths retrieved by a core data predicate, zipping and uploading each file per iteration of the loop, about 4 or 5000 files in total. (The technique I use for zipping is basically the one described here: Best way to loop through a lot of files and zip each one separately?) Currently, I’m running the app in debug mode from Xcode. After a little while, with each iteration of the loop, not only does the app seem to run slower, Xcode does too – indeed, my whole computer becomes less responsive. I’m now researching general rules for efficient loops involving NSStrings, NSArrays, NSNotifications, etc but I’m also hoping someone could also point out obvious bottlenecks and inefficiencies in my code. Although my code uses garbage collection, I tried putting in release calls with the small hope that might make a difference though my understanding is that it shouldn’t. Here is an abridged version of my code with just the important parts:

//Zip paths saved in Core Data and upload them to a server
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

//get path of the shell script that zips a file
NSString *zipShellScript = [[NSBundle mainBundle] pathForResource:@"backupzipper"
                                                           ofType:@"sh"];

//get array of File managed objects related to User managed object
NSArray *filesToUpload = [User files];

//args for the shell script
//NSString *sourceFilePath = [NSString string];
//NSString *targetFilename = [NSString string];
//NSString *targetFilePath = [NSString string];

//the following appears to make a big improvement
NSString *sourceFilePath = [[NSString alloc] initWithString:[NSString string]];
NSString *targetFileName = [[NSString alloc] initWithString:[NSString string]];
NSString *targetFilePath = [[NSString alloc] initWithString:[NSString string]];

NSError *error;

for (File *file in filesToUpload) //loop through array of File managed objects
{
    NSTask *task = [[NSTask alloc] init];

    sourceFilePath = file.path;
    targetFilename = [NSString stringWithFormat:@"%@.zip",
                      [[sourceFilePath lastPathComponent]
                       stringByDeletingPathExtension]]; //e.g. misc.doc will produce misc.zip

    [task setArguemnts:[NSArray arrayWithObjects:zipShellScript,
                        sourceFilePath, //full path of file to zip e.g. /Users/stifin/documents/misc.doc
                        workingDirectory, //where the zip will be placed e.g. /Users/stifin/Library/Application Support/MyApp/
                        targetFilename,  //filename after zipping e.g. misc.zip
                        nil]];
    [sourceFilePath release]; //my attempt to reduce memory usage even though this is garbage collected app
    [targetFilename release];

    [task setLaunchPath@"/bin/sh"];
    [task setStandardInput:[NSPipe pipe]]; //fixes odd behaviour where NSLog no longer works after NSTask

    //do zip
    [task launch];
    [task waitUntilExit];

    if ([task terminationReason] == NSTaskTerminationReasonExit)
    {
        [task release]; //doubt this helps

        [self uploadFile:targetFilePath]; //method just sleeps for 1 sec to simulate upload time

        file.dateUploaded = [NSDate date];
        error = nil;
        if ([context save:&error])
            NSLog(@"Saved ok");
        else
            NSLog(@"Save error: %@", [error localizedDescription]);

        //delete zip from working directory
        [self cleanUpFile:targetFilePath];
        [targetFilePath release]; //doubt this helps

        //send notification of file processed
        info = [NSDictionary dictionaryWithObjectsAndKeys:
                file.filename, @"name",
                file.sizeBytes, @"size",
                [NSNumber numberWithBool:YES], @"success",
                nil];
        [nc postnNotificationName:@"FileProcessed" object:nil userInfo:info];
        [info release]; //doubt this helps
    }
    else {
        //handle task failure
    }
}
[nc removeObserver:self];
  • 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-05-23T17:46:45+00:00Added an answer on May 23, 2026 at 5:46 pm

    As noted by the documentation, -release is ignored. If you want to collect immediately, use [[NSGarbageCollector defaultCollector] collectIfNeeded]; or, even -collectExhaustively. Don’t do this every trip through the loop, just every hundred or so.

    Of course all this assumes you’ve used Instruments to make sure this is the issue. You haven’t said whether you’ve done so, which leads me to believe you’re making assumptions. The problem might just be elsewhere (though I agree this is by far the most likely candidate – GC + many allocations in a tight loop needs some extra care). Not a good way to work. Measure first, then optimize.

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

Sidebar

Related Questions

I'm looping through a zip file trying to add the file name of each
Im having some trouble looping through some xml data. The xml file is structured
I'm trying to feed an array with fscanf() while looping through a file containing
I am looping through an array of strings, such as (1/12/1992 apple truck 12/10/10
I'm looping through an associative array with a foreach. I'd like to be able
I am looping through a large text file and im looking for lines that
I'm looping through multiple checkboxes with the same class with function each() and inside
I am looking through a data file containing both words and numbers, and I
Im trying to loop through a json files' object array to access its variables'
I have a phpmailer that attaches files by looping through an array of files

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.