我有一个singelton类,用于从Web服务中提取和存储所有数据。该类有一个NSArray和一个存储数据的NSDictionary。然后我想使用这些数据在其他视图中填充tableviews。
在我的一个视图中,在View Did Load方法中,我告诉单例从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
答案 0 :(得分:1)
api_calls = [[[NSDictionary alloc] initWithObjects:objs forKeys:keys] retain];
不保留它(您已经是该对象的所有者)。
至于崩溃 - 发布堆栈跟踪。
编辑:可能发现:
if (call == @"memberlist") {
memberData = [[NSDictionary alloc] init]; //remove this (it is redundant and causes leak)
memberData = [json JSONValue]; //retain this
}