views:

44

answers:

1

I have this class
loader.h

#import <Foundation/Foundation.h>
@class Reachability;
@interface loader : NSObject {

}
- (void)startLoading;
@end

loader.m

#import "loader.h"
#import "Reachability.h"
@implementation loader
- (void)startLoading{
    NSLog(@"Check network");
}
@end

Is the code bellow the correct way to include the above class and use it

in my .h file I put

@class loader;

and in the .m file

#import "loader.h"

and I use this code

- (void)viewDidLoad {
loader *myLoader = [loader alloc];
[myLoader startLoading];
[super viewDidLoad];
}

is this the correct way? to use a class??

I want loader class to be in change and check for internet connectivity and I try to start from somewhere :) thanks in advance :)

+1  A: 

It is fine for you to do so, except some parts:

  • You are leaking the myLoader by not releasing or auto release it

  • You may need an init method so that you can populate some data (it is a convention for me not an error)

vodkhang
You may not need an init, but you still need to call it so NSObject's init gets to run...
Stripes
Yeah, you may need to call NSObject init but in terms of theory, you don't really need
vodkhang
So in order to fix this and be 100% correct is to alloc and init and after calling my method to release it [[loader alloc] init] and then after that [loader release]. Am i right?
auslander
Let me ask you more few questions: what do you intend your loader to do. Does that job in the main thread or in another thread. If it is in main thread, you can possible release after you call. But, I suspect that in your particular case, because you are calling Reachability, which potentially runs on another thread, you should do it differently
vodkhang
You should declare a instance varible, a property, alloc init in the viewDidLoad and then call release in the dealloc method
vodkhang
I have a loading screen and I want during this view to create a class that will handle all init stuff (check internet connectivity, check online server version data, check data version with local data, update local data if needed that's all). So I believe that it will be my main thread. I will wait this to finish and then move on with the loading views. I should probably create a protocol to handle better the return data but right now I use NSNotificationCenter cause I find it a little bit easier :). Thanks for your tips again :)
auslander
Another advice: don't do all of your stuff on the main thread. Because if you do all of that, the screen freeze, users cannot touch or do anything on your screen. No gesture is recognized
vodkhang
You can accept my answer by clicking on the V mark under my question
vodkhang
yeap :) thanks again for your help.
auslander