0

I'm using parse to store and retrieve data from and to my iOS app. My code is as follows.

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (!error) {

        for (PFObject *item in objects) {

            self.postPlace.text =  [item objectForKey:@"place"];
        }
    }

    else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

However, on first view, I want the following code to be:

self.postPlace.text =  nil;

And then the rest of the time:

self.postPlace.text =  [item objectForKey:@"place"];

How can I implement this? I've looked into viewDidLoad and viewDidAppear, but I'm a little stuck.

user3429966
  • 95
  • 1
  • 10

3 Answers3

1

Do something like this:

Declare a Instance variable of type BOOL, lets call it isFirst.

In your viewDidLoad make isFirst = FALSE;. Then do this in you viewWillAppear (Remember viewDidLoad called only in first time. But viewWillAppear get called each time you come into this viewController. So plan your code accordingly.).:

    if (!isFirst) {
        self.postPlace.text =  nil;
        isFirst = !isFirst;
    }
    else{
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

            if (!error) {

                for (PFObject *item in objects) {
                    static BOOL flag = YES;
                    if (flag) {
                        self.postPlace.text = nil;
                    } else {
                        self.postPlace.text = [item objectForKey:@"place"];
                        flag = NO;
                    }
                }
            }

            else {
                NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
        }];
    }

Hope this helps ... :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
  • Why `static` instead of an instance variable? This limits the number of instances you can have of this class. – trojanfoe Apr 02 '14 at 15:34
  • Can you explain? It will be helpful for me. – Rashad Apr 03 '14 at 02:33
  • There can only be one instance of a `static` variable per-process, so using one limits the number of instances you can have of a class (to one). Instead use an instance variable and then you can have as many instances of this class as you like. – trojanfoe Apr 03 '14 at 05:09
  • @trojanfoe Thanks man, btw my global i mean class variable that can accessable by all methods. – Rashad Apr 03 '14 at 05:11
  • Again, I don't see the need for a global/class/static-scoped variable at all. An instance variable is simple/obvious/self-documenting. – trojanfoe Apr 03 '14 at 05:12
  • Okay. How do you determine if you came to this VC for the first time by an instance variable? I am asking bcz I hope I can learn a new thing here. :) – Rashad Apr 03 '14 at 05:15
  • `@interface ViewController : UIViewController { BOOL _firstTime; }` and them simply use `_firstTime` instead of that `static` variable you suggested. – trojanfoe Apr 03 '14 at 05:16
  • My bad. In my post I mean that. I just could not describe it in word. :( – Rashad Apr 03 '14 at 05:18
  • @trojanfoe `There can only be one instance of a static variable per-process`, could you give some proof or reference on this? I've never heard of this before. – sunkehappy Apr 03 '14 at 05:21
  • @Rashad My example is incorrect unless `_firstTime` is initialised to `YES` during class initialisation. It might be better to use a variable called `_setPostPlaceNil` and leave that to its default value of `NO` (and reverse the logic of its use) if the view controller doesn't initialise other variables. – trojanfoe Apr 03 '14 at 06:25
  • @sunkehappy A `static` local variable is effectively a global variable (except in scope) so there can be only one per-process (as with a global variable). I am surprised you are suggesting its use if you don't already know this. – trojanfoe Apr 03 '14 at 06:27
1

As I have been in protracted discussion with the authors of the other two (at the time of writing) answers about their suggested use of a static local variable, I will provide my own answer, with what I think is a much better solution.

The use of a static local variable to track if something has been done, or not, will limit the number of instances of the class to one, given there can be only one instance of a static local variable per-process. This might not be an issue for a View Controller, where only one instance might be required, however there are often cases where more than one instance of a view controller will be used and using a static local variable will cause a difficult-to-find bug later in development, especially as it's so inconspicuous.

My suggestion would be to track your behaviour using an instance variable, allowing multiple instances of the class and making it obvious you are doing so, as it appears in the class @interface shouting out its purpose (if named correctly).

Therefore:

@interface MyViewController : UIViewController
{
    BOOL _haveSetPostPlaceNil;
}

and in the code (there is no need to initialise it to NO unless you really feel the need), use:

for (PFObject *item in objects) {
    if (!_haveSetPostPlaceNil) {
        self.postPlace.text = nil;
        _haveSetPostPlaceNil = YES;
    } else {
        self.postPlace.text = [item objectForKey:@"place"];
    }
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

I suggest you use static BOOL flag to do this task.

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (!error) {

        for (PFObject *item in objects) {
            static BOOL flag = YES;
            if (flag) {
                self.postPlace.text = nil;
            } else {
                self.postPlace.text = [item objectForKey:@"place"];
                flag = NO;
            }
        }
    }

    else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];
sunkehappy
  • 8,970
  • 5
  • 44
  • 65
  • Why static instead of an instance variable? This limits the number of instances you can have of this class. – trojanfoe Apr 02 '14 at 15:34
  • @trojanfoe Because the view will appear more than once. And the OP only want to do something ONLY the first time. So static is needed. Another way is to an instance variable. But I think static variable is enough. – sunkehappy Apr 03 '14 at 00:59
  • @trojanfoe `This limits the number of instances you can have of this class.` Could you explain more? – sunkehappy Apr 03 '14 at 01:00
  • It's bad practise to use `static` for this as it will cause you a ugly bug one day when you forget about it. See the comment I left for the other answer. – trojanfoe Apr 03 '14 at 05:11