I've written a program that plays a queue of songs that is processed in a background task using delegates. I send responses back to AppController
to move a NSSlider
show progress of song being played and deleted the NSCollection View
entity (the song) that just played. Works great most of the time, but after 2 or three songs; the entity disappears but the remaining objects do not rearrange (move up on the screen) until I use the mouse or keyboard to bring the main screen into focus. I've used several suggestions to other similar questions and generally get the same response. The code I'm using now:
-(void) songPlayer:(SongPlayer *)player removeEntity:(DesktopEntity *)entity {
int i = 0;
NSIndexSet *indexes;
indexes = [NSIndexSet indexSetWithIndex:i];
NSLog(@"about to try");
@try {
// [_arrayController removeObjectAtArrangedObjectIndex:0];
[_seqFile removeObjectAtIndex:0];
[_songs removeObjectAtIndex:0];
[_myCollectionView setContent:_songs];
[_arrayController rearrangeObjects];
[_myCollectionView setNeedsDisplay:YES];
// [_arrayController rearrangeObjects];
// [_window makeKeyAndOrderFront:self];
// [_myCollectionView setNeedsDisplay:YES];
// [self.seqFile reloadData];
}
@catch (NSException *exception) {
NSLog(@"Ugly Ending Caught");
}
@finally {
NSLog(@"Each Time");
}
}
I've left comment code in to show other things I've tried to the same result. I believe it has to to with refreshing NSView, but not sure how.
my threads are set up as follows:
songPlayer.h
@protocol SongPlayerDelegate;
@interface SongPlayer : NSObject <SliderDelegate> {
id<SongPlayerDelegate> __unsafe_unretained delegate;
}
@property (nonatomic, assign) id<SongPlayerDelegate> delegate;
…
@end
@protocol SongPlayerDelegate <NSObject>
- (void)songPlayer:(SongPlayer *)player removeEntity:(id)enity;
@end
songPlayer.M
#import "SongPlayer.h"
@implementation SongPlayer
@synthesize delegate;
-(id) init {
if (self = [super init]) {
}
...
return self;
}
-(id) playMyQueue:(NSMutableArray *)q {
//playsong stuff
…
[self.delegate songPlayer:self removeEntity:entity];
}
appController.h
#import "SongPlayer.h"
@interface AppController : NSObject <SongPlayerDelegate>
@property (weak) IBOutlet NSCollectionView *myCollectionView;
@property IBOutlet NSArrayController *arrayController;
@property (strong) NSMutableArray *songs;
@property SongPlayer *myPlayer;
- (void)songPlayer:(SongPlayer *)player removeEntity:(id)enity;
appController.m
#import "AppController.h"
@implementation AppController
@synthesize songs = _songs;
-(void) awakeFromNib {
_songs = [[NSMutableArray alloc] init];
[_arrayController addObject:s];
…
_myPlayer = [[SongPlayer alloc] init];
…
}
-(IBAction)previewSong:(id)sender {
if ([_myPlayer qActive]) {
[(SongPlayer *)_myPlayer stopQueue];
[_songLabel setStringValue:@"playback has been stopped"];
} else {
if (![_myPlayer isDone]) {
[(SongPlayer *)_myPlayer stopSong];
[_songLabel setStringValue:@"playback has been stopped"];
} else {
[_queue removeAllObjects];
_myPlayer.delegate = self;
NSMutableArray *activeQueue =
[[NSMutableArray alloc] initWithArray:_seqFile];
[_myPlayer performSelectorInBackground:@selector(playMyQueue:)
withObject:activeQueue];
}
}
}
-(void) songPlayer:(SongPlayer *)player removeEntity:(DesktopEntity *)entity {
int i = 0;
NSIndexSet *indexes;
indexes = [NSIndexSet indexSetWithIndex:i];
NSLog(@"about to try");
@try {
[_arrayController removeObjectAtArrangedObjectIndex:0];
[_seqFile removeObjectAtIndex:0];
}
@catch (NSException *exception) {
NSLog(@"Ugly Ending Caught");
}
@finally {
NSLog(@"Each Time");
}
}