Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SmartiOS

macrumors newbie
Original poster
Jul 13, 2012
18
0
Hi, how to add activity indicator for this code i want to view activity indicator when copying the file until finish then give uialertview thank you.

Code:
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];*/
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];*/
    NSError *error;
    NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"track.mp3"];
    if ([fileManager fileExistsAtPath:txtPath] == NO) {
        NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"track" ofType:@"mp3"];
        [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
 
Last edited:
You're copying a single mp3 file - how long could that possibly take?

I suspect a spinning wheel will be adequate, if anything is necessary at all.

If not, you could roll your own method that copies bytes from one spot to another and keep track of how much has been done / not done and update a progress bar as you go. But that's probably a ton of work for minimal value.
 
my mp3 is 2 mb and doesn't matter whatever to be i just need how to add activity indicator in my project for something like copying file , downloading..etc which sample code to do that? thank you.
 
The activity indicator will only start to spin when you allow the main thread to run.

Your options to do this are:

On the main thread start the activity indicator spinning.
Use performSelectorOnMainThreadAfterDelay to call a method that executes the copy and then stops the activity indicator.

On the main thread start the activity indicator spinning
Run this code that does the copy on a background thread.
When it finishes notify the main thread to stop the activity indicator.

There are various ways to run this code in the background.

Running the copy code on the main thread is probably OK if it doesn't take more than a couple seconds.
 
this what i'm trying to do. how i can achieve this?

//--- copying files ---//
t5nf34.jpg

//--- file copied ---//
24euu79.jpg


any help would be appreciated.
 
I explain how to do this in my post #5. What don't you understand?

Yeah i have try your method but i didn't know how this will work, So take a look maybe there is something wrong.

Code:
{
    [activityIndicator startAnimating];
    [self performSelectorOnMainThread:@selector(doneParsing) withObject:nil afterDelay:0.3];

[COLOR="SeaGreen"]// my code  //[/COLOR]
[COLOR="SeaGreen"]....
....
....[/COLOR]

}

-(void)doneParsing
{
    sleep(5);
    [activityIndicator stopAnimating];
}
 
Yeah i have try your method but i didn't know how this will work...

It would be polite if you provided details on why it doesn't work. That is, please elaborate. What were you expecting to see? What did you see instead? Did you get any warnings / errors / crashes? What debugging, if any, have you done? What have you learned from it? Plus, you should provide more code and context. Is the method that calls [activityIndicator startAnimating] running on a background thread? Because you can't update the UI from the background.
 
i didn't see any of warnings or errors,crashes... i think that code is working but no activity indicator showing? in NSlog i see the file copied within 5 second using this sleep(5) any suggest about this? thanks.
 
Needs to be this:

Code:
-(void)begin
{
    [activityIndicator startAnimating];
    [self performSelectorOnMainThread:@selector(startParsing) withObject:nil afterDelay:0];
}

-(void)startParsing
{
// my code  //
....
....
....

    [activityIndicator stopAnimating];
}

unless begin is running on a background thread.
 
Sorry, i have edit since code was on warning:
Code:
[self performSelectorOnMainThread:@selector(doneParsing) withObject:nil afterDelay:0.3];

EDIT with your last Add:
i see is running on a background thread and Working without errors. but i need to show activity indicator that showing in main view?

Code:
{
    [activityIndicator startAnimating];
    [self performSelectorOnMainThread:@selector(doneParsing) withObject:nil waitUntilDone:YES];
}

-(void)doneParsing
{
[COLOR="SeaGreen"]// my code  //
....
....
....[/COLOR]
    
    sleep(5);
    [activityIndicator stopAnimating];
}
 
If you want to run the copy on the background thread then it needs to be more like this. Remember that you can't access the UI from the background. Also you should probably not access ivars on your background thread. Also you should consider cancelling of the parse task and what happens if the user taps the back button while the background task is still running.

Code:
-(void)beginOnMainThread
{
    [activityIndicator startAnimating];

// start background task here

}

-(void)startParsingOnBackgroundThrad
{
// my code  //
....
....
....
[self performSelectorOnMainThread:@selector(doneParsing) withObject:resultsOfParsing afterDelay:0];
}

-(void)doneParsing
{
// Update UI with results of parsing
  [activityIndicator stopAnimating];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.