objective-cpathdocumentsdirectory

Path to file is modified by another view controller


I have 2 view controllers that send requests to a web service. Once the data is received, data is saved to files in the Document folder.

These are the 2 VC:

Live_VC:

#import "FV_Live_ViewController.h"

@interface FV_Live_ViewController ()
@end

@implementation FV_Live_ViewController

NSArray  *paths;
NSString *documentsDirectory;
NSString *path;

- (void)viewDidLoad {

paths              = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths firstObject];

[self sendRequest];    // first request of data ("Live" data)
}

- (void)sendRequest {
// other code

// request of "Live" data
[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {
    if (success) {
        // other code

        NSString *filename = [NSString stringWithFormat:@"months.plist"];   
        path = [documentsDirectory stringByAppendingPathComponent:filename];    // path = "...\months.plist"

        // second request of data (monthly data) if months.plist doesn't exists
        if (![[NSFileManager defaultManager] fileExistsAtPath: path]) {
            [self sendMonthRequest];
        }
    }
}];
}    

- (void)sendMonthRequest {

[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {            
    if (success) {
        // other code

        [monthlyArray writeToFile: path atomically:YES];    // path should be "...\months.plist" while it is "...\yesterday.plist"
    }
}];
}

@end

Today_VC:

#import "FV_Today_ViewController.h"

@interface FV_Today_ViewController ()
@end

@implementation FV_Today_ViewController

NSArray  *paths;
NSString *documentsDirectory;
NSString *path;

- (void)viewDidLoad {

paths              = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths firstObject];

[self sendRequest];        // first call of "sendRequest" to get today data
}

- (void)sendRequest {
//  other code

// request of "Today" data
[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {
    if (success) {       
        //  other code

        NSString *filename = [NSString stringWithFormat:@"today.plist"];
        path = [documentsDirectory stringByAppendingPathComponent:filename];    // path = "...\today.plist"

        [dataDictionary writeToFile: path atomically:YES];      // save data to today.plist

        // second call of "sendRequest" to get yesterday data only if yesterday.plist doesn't already exists)           
        filename = [NSString stringWithFormat:@"yesterday.plist"];
        path = [documentsDirectory stringByAppendingPathComponent:filename];    // path = "...\yesterday.plist"
        if (![[NSFileManager defaultManager] fileExistsAtPath:path])
            [self sendRequest];
    }
}];
}

@end

In each VC I'm using an NSString ("path") to store the path but the problem is that in the "sendMonthRequest" method (Live_VC) the value of the path is the value set in the other VC (Today_VC). How can it be possible ? How can the value of an NSString in the first VC be changed by the second VC ?

Thanks, Corrado


Solution

  • You're getting this result because you made "path" a global variable by putting it at the top of the file. Surround it by curly braces, and then it will be a normal ivar,

    @implementation FV_Today_ViewController {
    
    NSArray  *paths;
    NSString *documentsDirectory;
    NSString *path;
    }