Exemplo n.º 1
0
		Task<string> createAlbumAndSaveLocalIdentifier ()
		{
			var tcs = new TaskCompletionSource<string> ();

			string collectionLocalIdentifier = null;

			PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => {

				var request = PHAssetCollectionChangeRequest.CreateAssetCollection("bugTrap");

				collectionLocalIdentifier = request.PlaceholderForCreatedAssetCollection.LocalIdentifier;
			
			}, (success, error) => {

				if (success && !string.IsNullOrEmpty(collectionLocalIdentifier)) {

					var keychain = new Keychain<TrapAlbum> (new TrapAlbum ());

					// ensure the album with that localIdentifier is 
					var collectionResult = PHAssetCollection.FetchAssetCollections(new [] { collectionLocalIdentifier }, null);
					if (collectionResult != null) {

						var collection = collectionResult.firstObject as PHAssetCollection;
						if (collection != null) {

							albumLocalIdentifier = collectionLocalIdentifier;

							if (!tcs.TrySetResult(albumLocalIdentifier)) {
								var ex = new Exception ("createAlbumAndSaveLocalIdentifier Failed");
								tcs.TrySetException(ex);
								// Log.Error(ex);
							}

							keychain.StoreKeyValues(new Dictionary<DataKeys, string> {
								{ DataKeys.LocalIdentifier, albumLocalIdentifier }
							});
						}
					} else {
						var ex = new Exception ("createAlbumAndSaveLocalIdentifier Failed");
						tcs.TrySetException(ex);
						// Log.Error(ex);
					}
				} else {
					var ex = new Exception ("createAlbumAndSaveLocalIdentifier Failed");
					tcs.TrySetException(ex);
					// Log.Error(ex);
				}
			});

			return tcs.Task;
		}