Skip to content

Commit

Permalink
Safer setDatabase
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.code.sf.net/p/osirix/code/osirix@10876 4e9e6e3d-4551-47eb-9dbe-a6127f9400a1
  • Loading branch information
rossetantoine committed Jan 25, 2013
1 parent fac173c commit 4c9a3df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 32 deletions.
6 changes: 4 additions & 2 deletions BrowserController+Sources.m
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,10 @@ -(void)setDatabaseFromSourceIdentifier:(DataNodeIdentifier*)dni
{
DicomDatabase* db = [dni database];

if (db)
[self setDatabase:db];
if (db)
{
[self performSelector: @selector(setDatabase:) withObject: db afterDelay: 0.01];
}
else
if ([dni isKindOfClass:[LocalDatabaseNodeIdentifier class]])
[self initiateSetDatabaseAtPath:dni.location name:dni.description];
Expand Down
8 changes: 5 additions & 3 deletions DicomImage.m
Original file line number Diff line number Diff line change
Expand Up @@ -947,17 +947,19 @@ + (NSString*) dbPathForManagedContext: (NSManagedObjectContext *) c
NSPersistentStoreCoordinator *sc = [c persistentStoreCoordinator];
NSArray *stores = [sc persistentStores];

if( [stores count] != 1)
if( stores == nil)
N2LogStackTrace( @"*** warning stores == nil");

else if( [stores count] != 1)
{
NSLog( @"*** warning [stores count] != 1 : %@", stores);
N2LogStackTrace( @"*** warning [stores count] != 1 : %@", stores);

for( id s in stores)
NSLog( @"%@", [[[sc URLForPersistentStore: s] path] stringByDeletingLastPathComponent]);
}
return [[[sc URLForPersistentStore: [stores lastObject]] path] stringByDeletingLastPathComponent];
}


-(NSString*) completePathWithDownload:(BOOL) download supportNonLocalDatabase: (BOOL) supportNonLocalDatabase
{
[self.managedObjectContext lock];
Expand Down
82 changes: 55 additions & 27 deletions browserController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,12 @@ -(void)_observeDatabaseAddNotification:(NSNotification*)notification
[self outlineViewRefresh];
[self refreshAlbums];

// for( id obj in matrixViewArray)
// {
// if( [obj isFault])
// NSLog( @"[obj isFault]");
// }

#ifndef OSIRIX_LIGHT
//If PACS On-Demand is activated, check if a local study has more or same number of images of a distant study
NSMutableArray *patientStudies = [NSMutableArray array];
Expand Down Expand Up @@ -1300,13 +1306,21 @@ -(void)setDatabase:(DicomDatabase*)db
{
[[db retain] autorelease]; // avoid multithreaded release

if( [NSThread isMainThread] == NO)
N2LogStackTrace( @"setDatabase MUST be performed on MAIN thread");

if (_database != db)
{
@try
{
[self willChangeValueForKey:@"database"];

[matrixLoadIconsThread cancel];
@synchronized( previewPixThumbnails)
{
[matrixLoadIconsThread cancel];
[matrixLoadIconsThread release];
matrixLoadIconsThread = nil;
}

[self saveLoadAlbumsSortDescriptors];

Expand Down Expand Up @@ -3225,9 +3239,6 @@ - (NSArray*) childrenArray: (id)item onlyImages: (BOOL)onlyImages

if( [item isFault] || [item isDeleted])
{
if( [item isFault])
NSLog( @"----- isFault - childrenArray : we have to refresh the outlineView...");

if( [item isDeleted])
NSLog( @"----- isDeleted - childrenArray : we have to refresh the outlineView...");

Expand All @@ -3236,6 +3247,9 @@ - (NSArray*) childrenArray: (id)item onlyImages: (BOOL)onlyImages
if( [item isFault])
item = [self.database objectWithID: [item objectID]];

if( [item isFault])
NSLog( @"----- isFault - childrenArray : we have to refresh the outlineView...");

if( [item isFault] || [item isDeleted] || item == nil)
return [NSArray array];
}
Expand Down Expand Up @@ -4564,12 +4578,12 @@ - (void)outlineViewSelectionDidChange: (NSNotification *)aNotification
separateThread = NO;
}

[matrixLoadIconsThread cancel];
[matrixLoadIconsThread release];
matrixLoadIconsThread = nil;

@synchronized( previewPixThumbnails)
{
[matrixLoadIconsThread cancel];
[matrixLoadIconsThread release];
matrixLoadIconsThread = nil;

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: _database, @"DicomDatabase", [files valueForKey:@"objectID"], @"objectIDs", [NSNumber numberWithBool: imageLevel], @"imageLevel", previewPix, @"Context", _database, @"DicomDatabase", nil];
if( separateThread)
{
Expand Down Expand Up @@ -7595,6 +7609,11 @@ - (void) initAnimationSlider
}
else if([[aFile valueForKey:@"type"] isEqualToString:@"Study"])
{
id item = [matrixViewArray objectAtIndex: [cell tag]];

if( [item isFault])
item = [self.database objectWithID: [item objectID]];

NSArray *images = matrixViewArray.count? [self imagesArray: [matrixViewArray objectAtIndex: [cell tag]]] : nil;

if( [images count])
Expand Down Expand Up @@ -8020,7 +8039,6 @@ - (IBAction) matrixDoublePressed:(id)sender

-(void) matrixInit:(long) noOfImages
{

@synchronized( previewPixThumbnails)
{
[previewPix release]; previewPix = nil;
Expand Down Expand Up @@ -8597,6 +8615,9 @@ - (void)matrixLoadIcons: (NSDictionary*)dict

@synchronized( previewPixThumbnails)
{
if( [[NSThread currentThread] isCancelled])
return;

tempPreviewPixThumbnails = [[previewPixThumbnails mutableCopy] autorelease];
tempPreviewPix = [[previewPix mutableCopy] autorelease];
}
Expand All @@ -8617,17 +8638,20 @@ - (void)matrixLoadIcons: (NSDictionary*)dict
_timeIntervalOfLastLoadIconsDisplayIcons = now;
@synchronized( previewPixThumbnails)
{
if( previewPix == context)
if( [[NSThread currentThread] isCancelled] == NO)
{
[previewPixThumbnails removeAllObjects];
[previewPixThumbnails addObjectsFromArray: tempPreviewPixThumbnails];

[previewPix removeAllObjects];
[previewPix addObjectsFromArray: tempPreviewPix];
if( previewPix == context)
{
[previewPixThumbnails removeAllObjects];
[previewPixThumbnails addObjectsFromArray: tempPreviewPixThumbnails];

[previewPix removeAllObjects];
[previewPix addObjectsFromArray: tempPreviewPix];
}
}
}

if( [NSThread isMainThread] == NO)
if( [NSThread isMainThread] == NO && [[NSThread currentThread] isCancelled] == NO)
[self performSelectorOnMainThread:@selector(matrixDisplayIcons:) withObject:nil waitUntilDone:NO modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
}
Expand Down Expand Up @@ -8687,22 +8711,26 @@ - (void)matrixLoadIcons: (NSDictionary*)dict
[tempPreviewPix addObject: [[[DCMPix alloc] myinitEmpty] autorelease]];
}


@synchronized( previewPixThumbnails)
{
if( previewPix == context)
if( [[NSThread currentThread] isCancelled] == NO)
{
[previewPixThumbnails removeAllObjects];
[previewPixThumbnails addObjectsFromArray: tempPreviewPixThumbnails];

[previewPix removeAllObjects];
[previewPix addObjectsFromArray: tempPreviewPix];
if( previewPix == context)
{
[previewPixThumbnails removeAllObjects];
[previewPixThumbnails addObjectsFromArray: tempPreviewPixThumbnails];

[previewPix removeAllObjects];
[previewPix addObjectsFromArray: tempPreviewPix];
}
}

if( [NSThread isMainThread] == NO)
[self performSelectorOnMainThread:@selector(matrixDisplayIcons:) withObject:nil waitUntilDone:NO modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
else
[self matrixDisplayIcons: nil];
}

if( [NSThread isMainThread] == NO)
[self performSelectorOnMainThread:@selector(matrixDisplayIcons:) withObject:nil waitUntilDone:NO modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
else
[self matrixDisplayIcons: nil];
}
@catch (NSException* e)
{
Expand Down

0 comments on commit 4c9a3df

Please sign in to comment.