5

pracowałem z ALAssetsLibraryChangedNotification w iOS 6.x (szczególnie w wersji 6.0.1), a moje wyniki są sprzeczne z tym, co chciałbym spodziewaj się, że otrzymam w moich userinfo, w oparciu o to, co rozumiem z dokumentacji.ios 6.0.1 ALAssetsLibraryChangedNotification, próbując zrozumieć, co jest wysyłane

Oto mój kod do rejestracji zdarzeń:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assetsLibraryDidChange:) name:ALAssetsLibraryChangedNotification object:_library]; 

przetestować, idę do mojej biblioteki zdjęć i usunąć niektóre elementy, dodać kilka elementów.

Oto mój przewodnik.

- (void)assetsLibraryDidChange:(NSNotification *)note 
{ 
    NSDictionary* info = [note userInfo]; 
    NSLog(@"assetsLibraryDidChange calling syncPhotoInfoFromAssets, userInfo %@", info); 
    // If the user information dictionary is nil, reload all assets and asset groups. 
    if(note.userInfo==nil) { 
    [self syncPhotoInfoFromAssets]; 
    return; 
    } 

    // If the user information dictionary an empty dictionary, there is no need to reload assets and asset groups. 
    if(note.userInfo.count == 0) { 
    return; 
    } 

// If the user information dictionary is not empty, reload the effected assets and asset groups. For the keys used, see “Notification Keys.” 
    NSSet *updatedAssets = [info objectForKey:ALAssetLibraryUpdatedAssetsKey]; 
    NSSet *updatedAssetGroup = [info objectForKey:ALAssetLibraryUpdatedAssetGroupsKey]; 
    NSSet *deletedAssetGroup = [info objectForKey:ALAssetLibraryDeletedAssetGroupsKey]; 
    NSSet *insertedAssetGroup = [info objectForKey:ALAssetLibraryInsertedAssetGroupsKey]; 

    NSLog(@"updated assets:%@", updatedAssets); 
    NSLog(@"updated asset group:%@", updatedAssetGroup); 
    NSLog(@"deleted asset group:%@", deletedAssetGroup); 
    NSLog(@"inserted asset group:%@", insertedAssetGroup); 
    //further processing here 
} 

moje wyjście:

ALAssetLibraryUpdatedAssetGroupsKey = "{(\n assets-library://group/?id=736B6346-6DA2-4E43-8830-9C263B2D29ED\n)}"; 
    ALAssetLibraryUpdatedAssetsKey = "{(\n assets-library://asset/asset.JPG?id=A695208B-3546-4CCA-B539-B1D132A209B3&ext=JPG\n)}"; 
} 
2013-01-06 22:50:45.738 Olesi[25468:3613] updated assets:{(
    assets-library://asset/asset.JPG?id=A695208B-3546-4CCA-B539-B1D132A209B3&ext=JPG 
)} 
2013-01-06 22:50:45.738 Olesi[25468:3613] updated asset group:{(
    assets-library://group/?id=736B6346-6DA2-4E43-8830-9C263B2D29ED 
)} 
2013-01-06 22:50:45.739 Olesi[25468:3613] deleted asset group:(null) 
2013-01-06 22:51:06.658 Olesi[25468:3613] inserted asset group:(null) 

Po usuwanie i wstawianie album, spodziewam się, że otrzymane dane w obu ALAssetLibraryDeletedAssetGroupsKey i ALAssetLibraryInsertedAssetGroupsKey i nic w żadnej z ALAssetLibraryUpdatedAsset klawisz *. Jakieś pomysły? Zauważam, że nawet Apple's sample code, który słucha tego powiadomienia, nawet nie korzysta z kluczy, ale ponownie wylicza wszystkie zasoby bez względu na konkretny klucz (który pachnie, jakby nie ufał informacjom o powiadomieniu).

Odpowiedz

6

Jeśli nie masz odniesienia do usuwanej grupy, system operacyjny nie daje ci znać.

uzyskać poprawne zachowanie za pomocą następującego kodu:

#import "ROBKViewController.h" 
#import <AssetsLibrary/AssetsLibrary.h> 

@interface ROBKViewController() 

@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary; 
@property (nonatomic, strong) ALAssetsGroup *currentAssetGroup; 

- (void) handleAssetChangedNotifiation:(NSNotification *)notification; 

@end 

@implementation ROBKViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 
     _assetsLibrary = [ALAssetsLibrary new]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAssetChangedNotifiation:) name:ALAssetsLibraryChangedNotification object:_assetsLibrary]; 
    } 

    return self; 
} 

- (void) dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ALAssetsLibraryChangedNotification object:nil]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
    } failureBlock:^(NSError *error) { 
    }]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Notification handlers 

- (void) handleAssetChangedNotifiation:(NSNotification *)notification 
{ 
    NSLog(@"notification: %@", notification); 

    if ([notification userInfo]) { 
     NSSet *insertedGroupURLs = [[notification userInfo] objectForKey:ALAssetLibraryInsertedAssetGroupsKey]; 
     NSURL *assetURL = [insertedGroupURLs anyObject]; 
     if (assetURL) { 
      [self.assetsLibrary groupForURL:assetURL resultBlock:^(ALAssetsGroup *group) { 
       self.currentAssetGroup = group; 
      } failureBlock:^(NSError *error) { 

      }]; 
     } 
    } 

} 

@end 

pamiętać, że tylko dostać powiadomienie o grupie, która przypisana do self.currentAssetGroup.

+2

Dziękuję. Czytając http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Notification_Keys błędnie przeczytałem, że będę w stanie uzyskać lista adresów URL dla zagrożonych zasobów. Wydaje się, że mamy listę zasobów tylko wtedy, gdy są * zmodyfikowane *, a dla wstawień lub usunięć - tylko powiadomienie o powiązanych zasobach * grupach *, co czyni to powiadomienie mniej użytecznym niż początkowo miałem nadzieję, zwłaszcza że w tej chwili mam do czynienia tylko z jedną grupą. –

+1

Tak, zgodnie z dokumentami: "wstawione lub skasowane zestawy ALA są identyfikowane przez unieważnienie zawierających ALAssetGroups." Jest to bardzo niefortunne i wydaje się, że musisz załadować ponownie całą grupę (np. Rolka z aparatu), jeśli zasób został dodany lub usunięty. – akaru

Powiązane problemy