Chris
Chris

Reputation: 4810

Singleton Property causing App crash

I have a singelton class which is used to pull and store all data from a Web Service. The class has an NSArray and an NSDictionary which stores the data. I then want to use this data to populate tableviews in other views.

In one of my views, within the View Did Load method, I tell the singleton to retrieve the data from the web service and store it (it successfully retrieves the data, I logged it). I then try to access the data like so:

[[ClubData initClubData] memberData];

If I try to use that to populate the table view, it crashes and keeps referencing some kind of view. One time it referenced CALayer, another time a WrapperView ??? Am I not doing something right

My Singleton:

#import "ClubData.h"
#import "JSON.h";

@implementation ClubData
@synthesize conn, response, url, api_calls, memberData, beerData, call;

static ClubData *globalClubData = nil;

#pragma mark -
#pragma mark Instance Methods

- (void)getBeerData {

}

- (void)getSingleBeer:(NSInteger *)beerID {
}

- (void)getClubData {

    //check for existing data
    if (memberData != nil)
        return;

    //init
    memberData = [[NSArray alloc] init];

    //create request
    call = @"memberlist";
    [self initRequest:@"memberlist"];

}

- (void)getMemberData:(NSInteger *)memberID {
}

- (void)parseData:(NSString *)json {

    //parse based on call type

    if (call == @"memberlist") {
        memberData = [[NSDictionary alloc] init];
        memberData = [json JSONValue];
    }

    //reset call
    [call release];
    call = nil;
}

#pragma mark -
#pragma mark Connection & Delegate

- (void)initRequest:(NSString *)type {

    //build url
    NSMutableString *rURL = [[NSMutableString alloc] initWithString:url];
    [rURL appendString:[api_calls objectForKey:type]];
    NSURL *tempURL = [[NSURL alloc] initWithString:rURL];
    [rURL release];

    //init request & create connection
    NSURLRequest *request = [[[NSURLRequest alloc] initWithURL:tempURL] autorelease];
    conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [tempURL release];

    //init response
    response = [[NSMutableData alloc] init];

}

//receive data
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [response appendData:data];
}

//connection complete
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    //release conn
    [conn release];

    //parse JSON
    NSString *json = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    [response release];

    //parse
    [self parseData:json];
    [json release];

}

//connection failed
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" 
                                                    message:@"Unable to connect to network. Network required to load data."
                                                    delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];    
}

#pragma mark -
#pragma mark Singleton Control

+ (ClubData *)initClubData {
    if (globalClubData == nil) {
        @synchronized(self) {
            globalClubData = [[super allocWithZone:NULL] init];
        }
    }
    return globalClubData;
}

- (id)init {
    if (self = [super init]) {

        //set url
        url = [[[NSString alloc] initWithString:@"https://myurl.com/path/to/script.php?action=get_app_data"] retain];

        //possible calls
        NSArray *keys = [[NSArray alloc] initWithObjects:@"beerlist", @"singlebeer", @"memberlist", nil];
        NSArray *objs = [[NSArray alloc] initWithObjects:@"&subaction=beerlist", @"&subaction=singlebeer&beerID=", @"&subaction=memberlist", nil];
        api_calls = [[[NSDictionary alloc] initWithObjects:objs forKeys:keys] retain];

        [keys release];
        [objs release];

    }
    return self;
}

#pragma mark -
#pragma mark Lifecycle

- (void)dealloc {
    [conn release];
    [response release];
    [url release];
    [call release];
    [api_calls release];
    [super dealloc];
}

@end

Upvotes: 2

Views: 731

Answers (1)

Max
Max

Reputation: 16719

api_calls = [[[NSDictionary alloc] initWithObjects:objs forKeys:keys] retain];

do not retain it (you are already an owner of that object).

As for the crash - post the stack trace.

Edit: Probably found:

if (call == @"memberlist") {
    memberData = [[NSDictionary alloc] init]; //remove this (it is redundant and causes leak)
    memberData = [json JSONValue]; //retain this
}

Upvotes: 1

Related Questions