presence 3, problem with threads

2 vistas
Ir al primer mensaje no leído

terbine9287

no leída,
7 jul 2009, 10:30:47 p.m.7/7/2009
para iPhone Application Development Auditors
I have looked at the Flicker sample code for hints on getting the
threads to work in presence 3 but I am a little stumped. I guess this
is a question regarding using threads properly. My goal is that I want
to retrieve the images, etc for a list of user names when the program
starts up and show a little progress indicator when doing so, so that
the program doesn't hang. I've been having some issues with doing this
and I have found that this is difficult to debug. More specifically,
in my PersonListViewController, I have...

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self showLoadingIndicators];
[self beginLoadingData];
}

- (void)beginLoadingData {

for (int i = 0; i < [userNamesArray count]; i++) {
NSString *currentUser = [userNamesArray objectAtIndex:i];
UserInfoLoadingOperation *operation = [[UserInfoLoadingOperation
alloc] initWithTwitterUser:currentUser target:self action:@selector
(didFinishLoadingWithResult:)];

[operationQueue addOperation:operation];
[operation release];
}
}

- (void)didFinishLoadingWithResult:(NSDictionary *)result {

Person *p = [[Person alloc] init];

p.userName = [result objectForKey:@"user"];
NSString *string = [result objectForKey:@"url"];

NSURL *url = [NSURL URLWithString:string];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
p.img = image;

[personListArray addObject:p];

[p release];

[self.tableView reloadData];
}

However, in my UserInfoLoadingOperation class, I don't believe that
anything is every retrieved. If I put a break point right before it
gets added to the queue, it shows that everything is null except for
the user name, which is what I passed in to begin with.
Additionally, I get this error which makes the program not even run.

*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

I cannot place any break points within my UserInfoLoadingOperation
class because I get that error before I get a chance to look at it, so
I'm assuming the problem lies before that somewhere? Am I doing
something fundamentally wrong or what? I'm a bit confused and would
appreciate any advice.

Thanks

Sadat Rahman

no leída,
8 jul 2009, 4:48:08 a.m.8/7/2009
para iphone-appd...@googlegroups.com
I suspect the problem lies somewhere in your UITableView delegate /
datasource
methods. What I did notice when I quickly glanced at the source code you had
pasted is a memory leak for the image variable:

> UIImage *image = [[UIImage alloc] initWithData:data];
> p.img = image;

You need to release image once it is retained by p.img. Remember the
NARC rules.

Rudi Farkas

no leída,
8 jul 2009, 9:19:38 a.m.8/7/2009
para iphone-appd...@googlegroups.com
Concerning '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)' :

Perhaps you did not allocate the array ... declaring a NSMutableArray *arr; just creates a pointer initialized to nil, you need to allocate a NSMutableArray somewhere in init.

Rudi

terbine9287

no leída,
11 jul 2009, 4:40:40 p.m.11/7/2009
para iPhone Application Development Auditors


Ok, well apparently the problem was coming from the
cellForRowAtIndexPath. What would happen is that I would have
something similar to...

cell.textLabel.text = [personListArray objectAtIndex:indexPath.row]
userName];

For some reason it was saying that the personListArray contained 0
items when I NSLogged the count of it. However, in my threads when i
add something to it, it clearly was working fine and was definitely
not of size 0. Does anyone have an explanation of why this would
happen? Perhaps I was loading it in a separate thread? I thought that
was the problem but I tried that as well and still no luck. That is
why I am unable to load anything and the program crashes right away
with the Index (0) error.

Any advice would be helpful.
Thanks

terbine9287

no leída,
11 jul 2009, 6:27:34 p.m.11/7/2009
para iPhone Application Development Auditors


Just doing a little test, I found that cellForRowAtIndexPath was
executed before the list had anything added to it. But shouldn't that
not happen? Shouldn't that get called after viewWillAppear and all the
threads complete?
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos