Wait for file access to finish?

Discussion in 'iOS Programming' started by xArtx, Jul 25, 2013.

  1. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #1
    Hi Guys,
    It might sound like paranoia.
    I have an app that creates 16 user default keys on it's first launch which are
    integers saved as keys (probably could have been an array for that one),
    also about 8 settings bundle keys.
    Every subsequent launch it reads the values in all of those keys,
    then it loads a 512K file, some images, then an 880K file.

    What happens if the file system isn't ready when the program is running?
    Should I be delaying (maybe for first launch for the writing) between any of this activity?
    Cheers, Art.
     
  2. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #2
    Are you doing this asynchronously?
    Unless you are threading off these operations to write something that another thread needs to read there's no problem.

    The usual operations to read/write to persistent memory are all blocking so if the file system isn't ready the thread will just wait until it's ready.

    post some code will ya!
     
  3. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #3
    No matter, that's all I needed to know, thanks :)
    It's all done at once (viewdidload), and something like this:
    Code:
        for (int scnt = 0; scnt < 15; scnt++) { // cycle high scores
        NSString *scorekey = @" ";
        scorekey = [NSString stringWithFormat:@"score%i",scnt];
        [[NSUserDefaults standardUserDefaults] setInteger:score[scnt] forKey:scorekey];
        }
    
    is embarrassing if that was supposed to be an integer array saved as a single key file!
     
  4. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #4
    You are writing out individual integers to NSUserdefaults in the viewdidload from a c array?

    NSUserdefauls is generally used to store small bits of configs and shouldn't really be thought of the same as persisting to disk.

    You may want to look in to creating some kind of score object that you can reference or pass between controllers that you can also write to disk. As a file or perhaps to Core Data.

    Not to be picky but
    Code:
    NSString *scorekey = @" ";
    scorekey = [NSString stringWithFormat:@"score%i",scnt];
    
    The first line where you initialize a string with a blank space is unnecessary. You alloc and init a string with a blank space. Then next line you alloc and init a new string that replaces the first one.
    Change to
    Code:
    NSString *scorekey  = [NSString stringWithFormat:@"score%i",scnt];
    
    Or
    Code:
    NSString *scorekey;
    scorekey = [NSString stringWithFormat:@"score%i",scnt];
    
    In this case you know that scorekey _will_ be assigned some value before it is read so you don't have to nil it even.
     
  5. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #5
    Will do, thanks.
    I should (and will get there..) write it to a plain text file.
    I already have the complete high score table in verbose bytes with leading zeros ready for a fixed width file.
    Got a bit lazy there once it worked :(




     

Share This Page