10 Replies
      Latest reply on Sep 21, 2015 2:33 PM by dave_at_apple
      ayn Level 1 Level 1 (0 points)

        In my app I fetch a photo album by name:

         

            let fetchOptions = PHFetchOptions()
            fetchOptions.predicate = NSPredicate(format: "title = %@", "SomeAlbumName")
            let collection = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
        
        

         

        This works in iOS 8.x and iOS 9.0 GM, but in 9.1 it crashes with a predicate format error:


        *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate in fetch options: localizedTitle == "SomeAlbumName"'
        
        

         

        I couldn't find anything on this in the release notes and the docs. Am I missing something obvious? Appreciate any help.

        • Re: 9.1: Predicate in PHAssetCollection fetch options crash
          jfeldman Level 1 Level 1 (0 points)

          We are seeing this same issue in the production version of our application from users running iOS 9.1. Seems to be an Apple bug since it works correctly in all other versions < 9.1. We should probably open a radar for it

          • Re: 9.1: Predicate in PHAssetCollection fetch options crash
            mbryzinski Level 1 Level 1 (0 points)

            Hi, we've got the same issue. So I can suggest a workaround for you. Instead of fetching needed collection with a predicate, you can fetch all collections and enumerate them to find with needed title.

             

            __block PHAssetCollection *collection = nil;
            PHFetchResult *assetCollectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
            [assetCollectionFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection * obj, NSUInteger idx, BOOL *stop) {
               if([obj.localizedTitle isEqualToString:@"Your collection title"])
               {
                 *stop = YES;
                 collection = obj;
               }
            }];
            
            

             

            It works well.

             

            But I think we should send a bugreport.

            • Re: 9.1: Predicate in PHAssetCollection fetch options crash
              riccardoch Level 1 Level 1 (0 points)

              Same issue for me (iOS 9.1)

               

              Code:

               

              private func initializeAlbum() {
                  
                      let fetchOptions: PHFetchOptions = PHFetchOptions()
                      fetchOptions.predicate = NSPredicate(format: "title = %@", self.albumName)
                      let collection: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
                  
                      if let firstObject: AnyObject = collection.firstObject {
                          self.albumExists = true
                          self.assetCollection = collection.firstObject as! PHAssetCollection
                          if let ac = self.assetCollection {
                              self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
                          }
                          println("\(self.albumName) album exists")
                      }
                      else {
                          var albumPlaceHolder: PHObjectPlaceholder!
                      
                          PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                          
                              let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(self.albumName)
                              albumPlaceHolder = request.placeholderForCreatedAssetCollection
                          
                              }, completionHandler: { (success: Bool, error: NSError!) in
                              
                                  self.albumExists = success
                                  if success {
                                      let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceHolder.localIdentifier], options: nil)
                                      self.assetCollection = collection.firstObject as! PHAssetCollection
                                      if let ac = self.assetCollection {
                                          self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
                                      }
                                      println("\(self.albumName) album made")
                                  }
                                  else {
                                      println("Failed to create \(self.albumName) Album")
                                  }
                              
                          })
                      }
                  
                  }
              
              

               

              Error:

               

              'NSInvalidArgumentException', reason: 'Unsupported predicate in fetch options: title == "iFeel"'
              
              

               

               

              For the moment this is a possible solution (in Swift):

               

              let collection: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: nil)
              
              for var k = 0; k < collection.count; k++ {
                          let obj: AnyObject! = collection.objectAtIndex(k)
                          if obj.title == self.albumName {
                              self.albumExists = true
                              self.assetCollection = obj as! PHAssetCollection
                             if let ac = self.assetCollection {
                                  self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
                              }
                              println("\(self.albumName) album exists")
                              break
                          }
              }
              
              • Re: 9.1: Predicate in PHAssetCollection fetch options crash
                dave_at_apple Apple Staff Apple Staff (35 points)

                This is a bug in the seed and will be fixed in a future beta.