I am creating an iPhone application with ARC enabled, in this I am having a situation.
In each page of the application, a web service call is happening. And in this method I am removing all the objects from the arrays before adding new value from the server. All is working fine, but sometimes app is crashing at [self.myArray removeAllObjects]
.
I have set @property for myArray @property (strong, nonatomic) NSMutableArray myArray;
What I am thinking is, as I am using ARC, the object, myArray
, is got released at some point and I try to remove all the objects from the same array. That causing the crash, I am not sure but I don't see any other reason for this.
So, I was thinking about to check, whether the array is valid before I remove the objects in it. I wrote a sample code to check the different scenarios. Here it is:
NSMutableArray *testArray = [[NSMutableArray alloc]initWithObjects:@"1", @"2", @"3", @"4", nil];
if (testArray) {
NSLog(@"i am alive");
}
[testArray release];
if (testArray) { //here how to check whether this array is valid or not?
NSLog(@"i am alive: %@", testArray);
}else{
NSLog(@"I am dead");
testArray = [[NSMutableArray alloc]initWithObjects:@"5", @"6", @"7", @"8", nil];
}
[testArray release];
[testArray removeAllObjects];
I know it will cause crashes, but it is just for checking. Here, how can I check whether the array is valid? Is this is a right approach or something else?
Please guide me.
Thanks.
Actual Code:
- (void)getFriendsList{
BOOL netIsAvailable = [self connected];
if (netIsAvailable) {
@try {
NSString *accessToken = [self getAccessToken];
NSString *tokenEncoded = [accessToken stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSString *finalUrl = [NSString stringWithFormat:@"%@FriendsnSongs/%@",CommonWebServiceUrl,tokenEncoded];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:finalUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *currentResult = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// NSString* responseString = [[NSString alloc] initWithData:currentResult encoding:NSUTF8StringEncoding];
if (currentResult != nil) {
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:currentResult
options:kNilOptions
error:&error];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode];
NSMutableArray *match = [json valueForKey:@"FriendsnSongsResult"];
if (statusCode == 200) {
if (self.userGuidArray) {
[self.albumNameArray removeAllObjects]; //here got crash
[self.artistNameArray removeAllObjects];
[self.deviceNameArray removeAllObjects]; //here also got crash once
[self.nickNameArray removeAllObjects];
[self.userProfileImageArray removeAllObjects];
[self.songTitleArray removeAllObjects];
[self.songStatusArray removeAllObjects];
[self.userGuidArray removeAllObjects];
}
for(NSArray *player in match) {
[self.albumNameArray addObject:[(NSArray *)player valueForKey:@"AlbumName"]];
[self.artistNameArray addObject:[(NSArray *)player valueForKey:@"Artist"]];
[self.nickNameArray addObject:[(NSArray *)player valueForKey:@"NickName"]];
[self.userProfileImageArray addObject:[(NSArray *)player valueForKey:@"ProfileImage"]];
[self.songStatusArray addObject:[(NSArray *)player valueForKey:@"Status"]];
[self.songTitleArray addObject:[(NSArray *)player valueForKey:@"Title"]];
[self.userGuidArray addObject:[(NSArray *)player valueForKey:@"UserGuid"]];
[self.deviceNameArray addObject:[(NSArray *)player valueForKey:@"DeviceName"]];
}
//Start timer for updating the friends list
if (timerActivated)
[self performSelectorOnMainThread:@selector(backgroundFriendsListUpdate) withObject:nil waitUntilDone:NO];
//Update table if the table not in search mode
if (!isSearching)
[self performSelectorOnMainThread:@selector(updateTable) withObject:nil waitUntilDone:YES];
}
}
}
@catch (NSException *exception) {
NSLog(@"Exception: %@",exception.name);
}
}
[DejalActivityView removeView];
}
And the important point is, in this class a background thread is running in every 30 seconds. It will call this same method for refreshing the table.
As you suggested in the comments, using temporary array is a good idea. The following code will fill a temporary array and use it to replace your array in main thread.
NSMutableArray *tempAlbumArray = [NSMutableArray array];
//fill your array
[tempAlbumArray addObject:<#some object#>];
dispatch_async(dispatch_get_main_queue(), ^{
//replace your array with the new one
//this code will be executed in main thread
self.albumNameArray = tempAlbumArray;
});