• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
MLMediaLibrary sometimes does not call my KVO
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

MLMediaLibrary sometimes does not call my KVO


  • Subject: MLMediaLibrary sometimes does not call my KVO
  • From: Gabriel Zachmann <email@hidden>
  • Date: Thu, 22 Sep 2016 00:59:29 +0200

I am a bit at a loss about the MLMediaLibrary API.
Maybe, I haven't quite understood it yet, maybe something else is wrong.

I am writing a screen saver that accesses Photos' albums and the images referenced by them.
(Code excerpts follow at the end of this email.)

I create an array of the top level albums at startup time of the screensaver , using the KVO.
At runtime, I occasionally extract the images referenced by one of those albums.
Most of the time it works fine, except sometimes, my KVO never gets invoked, at which point my screensaver hangs,
because I had to stop the animation during that phase.

I have not found a pattern as to when this happens.
(it is not a deterministic album, nor a deterministic n-th time.)

Any suggestions, hints, insights, and pointers will be highly appreciated.

Best regards,
Gabriel.


The code snippets.  (variables ending with underscore are instance variables.)


This is what I do at startup time (in -initwithFrame:isPreview:)


   albums_ = [[NSMutableArray alloc] init];
   NSDictionary *options = @{
                             MLMediaLoadSourceTypesKey: @(MLMediaSourceTypeImage),
                             MLMediaLoadIncludeSourcesKey: @[MLMediaSourcePhotosIdentifier]
                             };
   mediaLibrary_ = [[MLMediaLibrary alloc] initWithOptions: options];
   [mediaLibrary_ addObserver: self
                       forKeyPath: @"mediaSources"
                          options: 0
                          context: (__bridge void *) @"mediaLibraryLoaded"];
   [mediaLibrary_.mediaSources objectForKey: MLMediaSourcePhotosIdentifier ];  // starts asynchronous loading


During runtime, this gets executed occasionally:


   album_ = albums_[album_id];
   [album_ addObserver: self
            forKeyPath: @"mediaObjects"
               options: 0
               context: @"mediaObjects"];
   [album_ mediaObjects];


I have checked using log message that addObserver: does actually get executed.


And this is my key value observer:


- (void) observeValueForKeyPath: (NSString *) keyPath   ofObject: (id) object
                        change: (NSDictionary *) change context: (void *) context
{
   MLMediaSource * mediaSource = [mediaLibrary_.mediaSources objectForKey: MLMediaSourcePhotosIdentifier];
   if ( context == (__bridge void *) @"mediaLibraryLoaded" )
   {
       [mediaSource addObserver: self
                     forKeyPath: @"rootMediaGroup"
                        options: 0
                        context: (__bridge void *) @"rootMediaGroupLoaded"];
       [mediaSource rootMediaGroup];                                           // start next phase: load groups
   }
   else if ( context == (__bridge void *) @"rootMediaGroupLoaded" )
   {
       MLMediaGroup *albums = [mediaSource mediaGroupForIdentifier: @"TopLevelAlbums"];
       for ( MLMediaGroup * album in albums.childGroups )
       {
           NSString * albumName = [album.attributes objectForKey: @"name"];
           [self logMessage: [NSString stringWithFormat: @"album name = %@", albumName] asError: NO];
           if ( albumName )
               [albums_ addObject: album ];
       }
   }
   else if ( context == (__bridge void *) @"mediaObjects" )
   {
       NSArray * mediaObjects = album_.mediaObjects;
       for ( MLMediaObject * mediaObject in mediaObjects )
       {
           if ( mediaObject.mediaType != MLMediaTypeImage )
               // we still get movies as mediaObjects, which might be contained in a Photos album
               continue;

           NSURL * url  = mediaObject.URL;
           [photoPaths_ addObject: url.path];
       }
   }
}


Again, I checked with a log message that the KVO never gets invoked in the case when the screen saver hangs
(i.e., I am positive the hanging is not caused by an infinite loop or similar in the KVO.)




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: MLMediaLibrary sometimes does not call my KVO
      • From: Graham Cox <email@hidden>
  • Prev by Date: Re: CALayer kCAGravityResizeAspectFill and kCAGravityTop
  • Next by Date: Re: MLMediaLibrary sometimes does not call my KVO
  • Previous by thread: Re: CALayer kCAGravityResizeAspectFill and kCAGravityTop
  • Next by thread: Re: MLMediaLibrary sometimes does not call my KVO
  • Index(es):
    • Date
    • Thread