adding activity indicator. [SOLVED]

Discussion in 'iOS Programming' started by SmartiOS, Mar 1, 2013.

  1. SmartiOS, Mar 1, 2013
    Last edited: Mar 7, 2013

    SmartiOS macrumors newbie

    Joined:
    Jul 13, 2012
    #1
    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];
     
  2. ArtOfWarfare macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    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.
     
  3. SmartiOS thread starter macrumors newbie

    Joined:
    Jul 13, 2012
    #3
    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.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Do you mean the networkActivityIndicator in the status bar or UIActivityIndicatorView?
     
  5. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    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.
     
  6. SmartiOS thread starter macrumors newbie

    Joined:
    Jul 13, 2012
    #6
    this what i'm trying to do. how i can achieve this?

    //--- copying files ---//
    [​IMG]
    //--- file copied ---//
    [​IMG]

    any help would be appreciated.
     
  7. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    I explain how to do this in my post #5. What don't you understand?
     
  8. SmartiOS thread starter macrumors newbie

    Joined:
    Jul 13, 2012
    #8
    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];
    }
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    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.
     
  10. SmartiOS thread starter macrumors newbie

    Joined:
    Jul 13, 2012
    #10
    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.
     
  11. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    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.
     
  12. SmartiOS thread starter macrumors newbie

    Joined:
    Jul 13, 2012
    #12
    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];
    }
     
  13. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    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];
    }
     

Share This Page