retain the value of an NSMutableArray?
I just got stuck. I have 2 view controllers one is "WatchListViewController
" and 2nd is "QuotesViewController
" first of all I am pushing from WatchListViewController
to QuotesViewController
and my action is written here.
-(IBAction)qoutes
{
QuotesViewController *nextController = [[QuotesViewController alloc] initWithQoutes:symbolForQoutes :exchangeForQoutes :appDelegate.s :code :exchange:exchangeCodeForQoutes];
[self.navigationController pushViewController:nextController animated:YES];
}
it works perfectly my initWithQoutes
method is also working good, then in ViewDidLoad
method I am sending some request to server via socket and it gives response and reponse is received in my function that is in WatchListViewController
:
void receiveData(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
char *buffer = (char *)CFDataGetBytePtr((CFDataRef)data); //getting data from server side in a char type veriable "buffer"
NSString *result = [NSString stringWithUTF8String:buffer]; // getting data in a string from my buffer
if (strstr(buffer, "QRES") != NULL) { //if this is a qoutes response
splits = [result componentsSeparatedByString:@"|"]; //separating string by "|"
body = [splits objectAtIndex:1]; // choosing index "1"
splits = [body componentsSeparatedByString:@","]; // separating by ","
QuotesViewController *quotesViewController = [[QuotesViewController alloc] init];
[quotesViewController initWithData:splits];
return;
}
}
response is received and it goes to the method initWithData
with a parameter array "splits" i receive this array in my initWithData and assign it to my other array (generalArray
) now general array is assigned to my tableView
but before it reloadData
of tableView its control goes back to receiveData
and return and come back again to my QuotesViewController
and reload table view but at that time my array (generalArray
) lost its value & values are not loaded in table view.
plz plz help me i got stuck here.
code of method initWithData:
-(void)initWithData:(NSArray *)_general
{
general = [[NSMutableArray alloc] init]; //initializing array that can be accessed out of the class
for 开发者_开发技巧(int i=0; i< [_general count]; i++) {
[general addObject:[_general objectAtIndex:i]]; }
NSLog(@"%@", general); [self generalValues];
}
-(void)generalValues{
[generalTableView reloadData];
}
You are creating a QuotesViewController
instance here in your C function as –
[quotesViewController initWithData:splits];
You are not storing a reference to this controller. It is highly likely that this instance of QuotesViewController
has the proper values in generalArray
. But since you are not storing it for later use, a reference is lost and a memory leak is created.
Now, if you go on to create another instance of QuotesViewController
, it will not have earlier generalArray
data as it is a new instance. Since I do not understand the flow of your program, I find it hard to suggest a proper way to go forward but it would be correct to save either the reference in the function or the array itself. If you save the reference, you can simply push it into the navigation controller of if you save the array, you can pass it in the init method.
EDIT
Since you are talking of affecting the same controller, you will need to pass the reference to the function.
void receiveData(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info, QuotesViewController *controller)
{
char *buffer = (char *)CFDataGetBytePtr((CFDataRef)data); //getting data from server side in a char type veriable "buffer"
NSString *result = [NSString stringWithUTF8String:buffer]; // getting data in a string from my buffer
if (strstr(buffer, "QRES") != NULL) { //if this is a qoutes response
splits = [result componentsSeparatedByString:@"|"]; //separating string by "|"
body = [splits objectAtIndex:1]; // choosing index "1"
splits = [body componentsSeparatedByString:@","]; // separating by ","
[controller initWithData:splits];
return;
}
}
Hopefully this should do it.
You should study memory management in Objective-C. The array you create with componentsSeparatedByString
is autoreleased by that method. So when you pass it into the initWithData
method of QuotesViewController, you need to retain it in that method with something like
generalArray = [splits retain];
Post your initWithData
code if you're not sure what to do.
In this code:
QuotesViewController *quotesViewController = [[QuotesViewController alloc] init];
[quotesViewController initWithData:splits];
you are initializing quotesViewController
twice. The second time you initialize it, you are not storing the return value, so your reference is lost. Instead, you should write
QuotesViewController *quotesViewController = [[QuotesViewController alloc] initWithData:splits];
Try that and see if it helps.
精彩评论