Skip to content

Commit

Permalink
Smarter scheduling of download operations, first leaks closed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Pigozzi committed Aug 12, 2009
1 parent 52fd94a commit 0fab77e
Show file tree
Hide file tree
Showing 10 changed files with 950 additions and 715 deletions.
12 changes: 9 additions & 3 deletions Entry.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ - (NSString *) imageRepresentationType
return IKImageBrowserNSImageRepresentationType;
}

-(void)imageDownloadCancel
{
__downloadOperation = nil;
}

- (id) imageRepresentation
{
if(!__img){ //image not cached in memory
Expand All @@ -103,10 +108,11 @@ - (id) imageRepresentation
if(!__img && !__downloadOperation){
MALHandler *mal = [MALHandler sharedHandler];
NSString * url = (NSString*) [self valueForKey:@"image_url"];
PGZCallback * callback = [[PGZCallback alloc] initWithInstance:self selector:@selector(scaledImageCallback:)];
__downloadOperation = [[ImageDownloadOperation alloc] initWithURL:url type:[[self entity] name]
PGZCallback * callback = [[[PGZCallback alloc] initWithInstance:self selector:@selector(scaledImageCallback:)] autorelease];
__downloadOperation = [[[ImageDownloadOperation alloc] initWithURL:url type:[[self entity] name]
entryid:[[self valueForKey:@"id"] intValue]
callback:callback];
callback:callback] autorelease];
__downloadOperation.__cancelled = [[[PGZCallback alloc] initWithInstance:self selector:@selector(imageDownloadCancel)] autorelease];
[mal.dl_queue addOperation:__downloadOperation];
}
return __img;
Expand Down
3 changes: 3 additions & 0 deletions ImageDownloadOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
NSString * __type;
NSInteger __id;
PGZCallback * __callback;
PGZCallback * __cancelled;
}

-(ImageDownloadOperation *) initWithURL:(NSString *) url type:(NSString *) type entryid:(NSInteger) entryid callback:(PGZCallback *) callback;
Expand All @@ -24,4 +25,6 @@
@property (assign) NSInteger __id;
@property (retain) PGZCallback * __callback;

@property (retain) PGZCallback * __cancelled;

@end
24 changes: 24 additions & 0 deletions ImageDownloadOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ @implementation ImageDownloadOperation
@synthesize __id;
@synthesize __callback;

@synthesize __cancelled;

-(ImageDownloadOperation *) initWithURL:(NSString *) url type:(NSString *) type entryid:(NSInteger) entryid callback:(PGZCallback *) callback
{
self = [super init];
Expand All @@ -31,7 +33,18 @@ -(ImageDownloadOperation *) initWithURL:(NSString *) url type:(NSString *) type

-(void)main
{
if(self.isCancelled){
[self.__cancelled perform];
return;
}

NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.__url]];

if(self.isCancelled){
[self.__cancelled perform];
return;
}

NSString * filename = [NSString
stringWithFormat:@"/Users/%@/Library/Application Support/iMAL/images/%@/%d.jpg",NSUserName(),self.__type,self.__id];
NSImage * image = [[[NSImage alloc] initWithData:fetchedData] autorelease];
Expand All @@ -44,7 +57,18 @@ -(void)main
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:filename atomically:NO];

if(self.isCancelled){
[self.__cancelled perform];
return;
}

[__callback performWithObject:image];
}

-(void)dealloc
{
[self.__cancelled perform];
[super dealloc];
}

@end
10 changes: 5 additions & 5 deletions InfoWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ -(IBAction)edit:(id)sender

PGZCallback * cb = [[[PGZCallback alloc] initWithInstance:self selector:@selector(editCallback)] autorelease];
MALHandler * mal = [MALHandler sharedHandler];
[mal.queue addOperation:[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb]];
[mal.queue addOperation:[[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb] autorelease]];
}

-(void)increaseEpisodeCallback:(Entry *)e
Expand Down Expand Up @@ -126,12 +126,12 @@ -(IBAction)increaseEpisodeCount:(id)sender
if([[[e entity] name] isEqual:@"anime"]){
[values setObject:[NSString stringWithFormat:@"%d", [[e valueForKey:@"my_episodes"] intValue]+1] forKey:@"episode"];
PGZCallback * cb = [[[PGZCallback alloc] initWithInstance:self selector:@selector(increaseEpisodeCallback:) argumentObject:e] autorelease];
[mal.queue addOperation:[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb]];
[mal.queue addOperation:[[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb] autorelease]];
}
if([[[e entity] name] isEqual:@"manga"]){
[values setObject:[NSString stringWithFormat:@"%d", [[e valueForKey:@"my_chapters"] intValue]+1] forKey:@"chapter"];
PGZCallback * cb = [[[PGZCallback alloc] initWithInstance:self selector:@selector(increaseChapterCallback:) argumentObject:e] autorelease];
[mal.queue addOperation:[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb]];
[mal.queue addOperation:[[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb] autorelease]];
}
}

Expand All @@ -144,12 +144,12 @@ -(IBAction)decreaseEpisodeCount:(id)sender
if([[[e entity] name] isEqual:@"anime"]){
[values setObject:[NSString stringWithFormat:@"%d", [[e valueForKey:@"my_episodes"] intValue]-1] forKey:@"episode"];
PGZCallback * cb = [[[PGZCallback alloc] initWithInstance:self selector:@selector(decreaseEpisodeCallback:) argumentObject:e] autorelease];
[mal.queue addOperation:[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb]];
[mal.queue addOperation:[[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb] autorelease]];
}
if([[[e entity] name] isEqual:@"manga"]){
[values setObject:[NSString stringWithFormat:@"%d", [[e valueForKey:@"my_chapters"] intValue]-1] forKey:@"chapter"];
PGZCallback * cb = [[[PGZCallback alloc] initWithInstance:self selector:@selector(decreaseChapterCallback:) argumentObject:e] autorelease];
[mal.queue addOperation:[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb]];
[mal.queue addOperation:[[[UpdateOperation alloc] initWithID:[[e valueForKey:@"id"] intValue] withType:[[e entity] name] values:values callback:cb] autorelease]];
}
}

Expand Down
6 changes: 6 additions & 0 deletions ListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "ListViewController.h"
#import "InfoWindowController.h"
#import "ListWindowController.h"
#import "MALHandler.h"

@implementation ListViewController

Expand Down Expand Up @@ -164,6 +165,11 @@ -(void) refreshZoom

-(void)constructPredicate
{
MALHandler * mal = [MALHandler sharedHandler];
[mal.dl_queue cancelAllOperations]; // this is to keep downloading first the images we are currently displaying
mal.dl_queue = [NSOperationQueue new];
[mal.dl_queue setMaxConcurrentOperationCount:2];

NSString * _search_pred = nil;
if(searchString)
_search_pred = [NSString stringWithFormat:@"(title like[c] \"*%@*\") OR (synonyms like[c] \"*%@*\")", searchString, searchString];
Expand Down
4 changes: 2 additions & 2 deletions MALHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static const int MALPlantoRead = MALPlantoWatch;
- (NSData *) search:(NSString *) query type:(NSString *) type;
- (NSData *) getList:(NSString *)type;

@property (retain, readonly) NSOperationQueue *queue;
@property (retain, readonly) NSOperationQueue *dl_queue;
@property (retain) NSOperationQueue *queue;
@property (retain) NSOperationQueue *dl_queue;

@end
14 changes: 10 additions & 4 deletions MALHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ - (id) init
{
self = [super init];
if (self != nil) {
self->queue = [NSOperationQueue new];
[self->queue setMaxConcurrentOperationCount:2];
self.queue = [NSOperationQueue new];
[self.queue setMaxConcurrentOperationCount:2];

self->dl_queue = [NSOperationQueue new];
[self->dl_queue setMaxConcurrentOperationCount:2];
self.dl_queue = [NSOperationQueue new];
[self.dl_queue setMaxConcurrentOperationCount:2];
}
return self;
}
Expand Down Expand Up @@ -125,4 +125,10 @@ - (NSData *) post:(NSString *) resource data:(NSData *) data{
return _r;
}

// using html parsing because Xinil is a lazy motherfucker
-(void)increaseRewatchedValue
{

}

@end
16 changes: 8 additions & 8 deletions SearchWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ -(IBAction) searchAnime:(id) sender
[animeSpinner setHidden:NO];
MALHandler * mal = [MALHandler sharedHandler];
NSString * type = @"anime";
PGZCallback * callback = [[PGZCallback alloc] initWithInstance:self selector:@selector(searchAnimeCallback:)];
[mal.queue addOperation:[[SearchOperation alloc] initWithQuery:[animeSearchField stringValue] withType:type callback: [callback autorelease]]];
PGZCallback * callback = [[[PGZCallback alloc] initWithInstance:self selector:@selector(searchAnimeCallback:)] autorelease];
[mal.queue addOperation:[[[SearchOperation alloc] initWithQuery:[animeSearchField stringValue] withType:type callback:callback]autorelease]];
}

-(void) searchAnimeCallback:(NSArray *) entries
Expand All @@ -83,8 +83,8 @@ -(IBAction) searchManga:(id) sender
[mangaSpinner setHidden:NO];
MALHandler * mal = [MALHandler sharedHandler];
NSString * type = @"manga";
PGZCallback * callback = [[PGZCallback alloc] initWithInstance:self selector:@selector(searchMangaCallback:)];
[mal.queue addOperation:[[SearchOperation alloc] initWithQuery:[mangaSearchField stringValue] withType:type callback: [callback autorelease]]];
PGZCallback * callback = [[[PGZCallback alloc] initWithInstance:self selector:@selector(searchMangaCallback:)] autorelease];
[mal.queue addOperation:[[[SearchOperation alloc] initWithQuery:[mangaSearchField stringValue] withType:type callback: callback] autorelease]];
}

-(void) searchMangaCallback:(NSArray *) entries
Expand All @@ -111,8 +111,8 @@ -(IBAction) addAnime:(id) sender
[values setObject:[NSString stringWithFormat:@"%@", [episodesField stringValue]] forKey:@"episode"];
[values setObject:[NSString stringWithFormat:@"%d", [[animeStatus selectedCell] tag]] forKey:@"status"];

PGZCallback * callback = [[PGZCallback alloc] initWithInstance:self selector:@selector(addAnimeCallback:)];
AddOperation * o = [[AddOperation alloc] initWithID:sm.__id withType:@"anime" values:values callback:callback];
PGZCallback * callback = [[[PGZCallback alloc] initWithInstance:self selector:@selector(addAnimeCallback:)] autorelease];
AddOperation * o = [[[AddOperation alloc] initWithID:sm.__id withType:@"anime" values:values callback:callback] autorelease];
o.__db = self.__db;
[mal.queue addOperation:o];
}
Expand Down Expand Up @@ -140,8 +140,8 @@ -(IBAction) addManga:(id) sender
[values setObject:[NSString stringWithFormat:@"%@", [volumesField stringValue]] forKey:@"volume"];
[values setObject:[NSString stringWithFormat:@"%d", [[mangaStatus selectedCell] tag]] forKey:@"status"];

PGZCallback * callback = [[PGZCallback alloc] initWithInstance:self selector:@selector(addMangaCallback:)];
AddOperation * o = [[AddOperation alloc] initWithID:sm.__id withType:@"manga" values:values callback:callback];
PGZCallback * callback = [[[PGZCallback alloc] initWithInstance:self selector:@selector(addMangaCallback:)] autorelease];
AddOperation * o = [[[AddOperation alloc] initWithID:sm.__id withType:@"manga" values:values callback:callback] autorelease];
o.__db = self.__db;
[mal.queue addOperation:o];
}
Expand Down
Loading

0 comments on commit 0fab77e

Please sign in to comment.