Objective c 导致应用程序崩溃的Singleton属性

Objective c 导致应用程序崩溃的Singleton属性,objective-c,xcode,ios4,singleton,Objective C,Xcode,Ios4,Singleton,我有一个singelton类,用于从Web服务中提取和存储所有数据。该类有一个NSArray和一个存储数据的NSDictionary。然后,我想使用此数据填充其他视图中的TableView 在我的一个视图中,在View-Did-Load方法中,我告诉singleton从web服务检索数据并存储它(它成功地检索了数据,我记录了它)。然后,我尝试如下方式访问数据: [[ClubData initClubData]memberData] 如果我尝试使用它来填充表视图,它会崩溃并继续引用某种视图。一次引

我有一个singelton类,用于从Web服务中提取和存储所有数据。该类有一个NSArray和一个存储数据的NSDictionary。然后,我想使用此数据填充其他视图中的TableView

在我的一个视图中,在View-Did-Load方法中,我告诉singleton从web服务检索数据并存储它(它成功地检索了数据,我记录了它)。然后,我尝试如下方式访问数据:

[[ClubData initClubData]memberData]

如果我尝试使用它来填充表视图,它会崩溃并继续引用某种视图。一次引用CALayer,另一次引用WrapperView???我做得不对吗

我的单身汉:

#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
不要保留它(您已经是该对象的所有者)

至于崩溃-发布堆栈跟踪

编辑:可能找到:

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

我很好奇静态GlobalClubData是否在init函数之外。我看到的示例在sharedInstance方法中声明了它,而在apple文档中,它是在外部声明的。所以我把它放在那里。老实说,我不知道。交易网站开发人员正在尝试学习一些新的东西。谢谢,保留该对象非常有效!不过,我还有一个问题。我在TableClass的viewWillLoad方法中调用该方法(检索数据)(以便在从未…查看该视图的情况下防止加载该方法)。如何检查数据是否已加载。因为当我调用[[self tableView]reloadData]时,由于没有加载数据,它不一定工作。这有意义吗?也许我应该发布一个新问题。在viewDidLoad中这样做而不是在viewWillLoadOk中这样做。这不是资源问题吗?我假设不在那里做,因为如果用户从未单击过该视图怎么办?它会毫无理由地加载这些数据。或者这不是什么大不了的事吗?TableClass是UIViewController的子类吗?利用此单例并具有table view对象的类是UITableViewController的子类
if (call == @"memberlist") {
    memberData = [[NSDictionary alloc] init]; //remove this (it is redundant and causes leak)
    memberData = [json JSONValue]; //retain this
}