public void FetchRecords (string recordType, Action<List<CKRecord>> completionHandler) { var truePredicate = NSPredicate.FromValue (true); var query = new CKQuery (recordType, truePredicate) { SortDescriptors = new [] { new NSSortDescriptor ("creationDate", false) } }; var queryOperation = new CKQueryOperation (query) { DesiredKeys = new [] { ValueField } }; var results = new List<CKRecord> (); queryOperation.RecordFetched = (record) => { results.Add (record); }; queryOperation.Completed = (cursor, error) => { if (error != null) { Console.WriteLine ("An error occured: {0}", error.Description); return; } InvokeOnMainThread (() => completionHandler (results)); }; privateDatabase.AddOperation (queryOperation); }
public void SetUp() { TestRuntime.AssertXcodeVersion(6, 0); TestRuntime.AssertSystemVersion(ApplePlatform.MacOSX, 10, 10, throwIfOtherPlatform: false); q = new CKQuery("Foo", NSPredicate.FromFormat("email = '@xamarin'")); op = new CKQueryOperation(q); }
CKQueryOperation CreateLoadBatchOperation() { CKQueryOperation queryOp = null; if (postCursor != null) { // If we have a cursor, go ahead and just continue from where we left off queryOp = new CKQueryOperation(postCursor); } else { CKQuery postQuery = CreatePostQuery(); queryOp = new CKQueryOperation(postQuery); } downloadingBatchStorage.Clear(); // This query should only fetch so many records and only retrieve the information we need queryOp.ResultsLimit = updateBy; queryOp.DesiredKeys = desiredKeys; queryOp.RecordFetched = OnPostFetched; queryOp.Completed = OnPostLoadingCompleted; queryOp.Database = PublicDB; return(queryOp); }
public void QueryForRecords(string referenceRecordName, Action <List <CKRecord> > completionHandler) { var recordId = new CKRecordID(referenceRecordName); var parent = new CKReference(recordId, CKReferenceAction.None); var predicate = NSPredicate.FromFormat("parent == %@", parent); var query = new CKQuery(ReferenceSubItemsRecordType, predicate) { SortDescriptors = new [] { new NSSortDescriptor("creationDate", false) } }; var queryOperation = new CKQueryOperation(query) { DesiredKeys = new [] { NameField } }; var results = new List <CKRecord> (); queryOperation.RecordFetched = (record) => results.Add(record); queryOperation.Completed = (cursor, error) => { if (error != null) { Console.WriteLine("An error occured: {0}", error.Description); return; } DispatchQueue.MainQueue.DispatchAsync(() => completionHandler(results)); }; publicDatabase.AddOperation(queryOperation); }
public void QueryForRecords(CLLocation location, Action <List <CKRecord> > completionHandler) { var radiusInKilometers = NSNumber.FromFloat(5f); var predicate = NSPredicate.FromFormat("distanceToLocation:fromLocation:(location, %@) < %f", new NSObject[] { location, radiusInKilometers }); var query = new CKQuery(ItemRecordType, predicate) { SortDescriptors = new [] { new NSSortDescriptor("creationDate", false) } }; var queryOperation = new CKQueryOperation(query) { DesiredKeys = new [] { NameField } }; var results = new List <CKRecord> (); queryOperation.RecordFetched = (record) => results.Add(record); queryOperation.Completed = (cursor, error) => { if (error != null) { Console.WriteLine("An error occured: {0}", error.Description); return; } DispatchQueue.MainQueue.DispatchAsync(() => completionHandler(results)); }; publicDatabase.AddOperation(queryOperation); }
public void QueryForRecords (CLLocation location, Action<List<CKRecord>> completionHandler) { var radiusInKilometers = NSNumber.FromFloat (5f); var predicate = NSPredicate.FromFormat ("distanceToLocation:fromLocation:(location, %@) < %f", new NSObject[] { location, radiusInKilometers }); var query = new CKQuery (ItemRecordType, predicate) { SortDescriptors = new [] { new NSSortDescriptor ("creationDate", false) } }; var queryOperation = new CKQueryOperation (query) { DesiredKeys = new [] { NameField } }; var results = new List<CKRecord> (); queryOperation.RecordFetched = (record) => results.Add (record); queryOperation.Completed = (cursor, error) => { if (error != null) { Console.WriteLine ("An error occured: {0}", error.Description); return; } DispatchQueue.MainQueue.DispatchAsync (() => completionHandler (results)); }; publicDatabase.AddOperation (queryOperation); }
public void FetchRecords(string recordType, Action <List <CKRecord> > completionHandler) { var truePredicate = NSPredicate.FromValue(true); var query = new CKQuery(recordType, truePredicate) { SortDescriptors = new [] { new NSSortDescriptor("creationDate", false) } }; var queryOperation = new CKQueryOperation(query) { DesiredKeys = new [] { NameField } }; var results = new List <CKRecord> (); queryOperation.RecordFetched = (record) => results.Add(record); queryOperation.Completed = (cursor, error) => { if (error != null) { Console.WriteLine("An error occured: {0}", error.Description); return; } InvokeOnMainThread(() => completionHandler(results)); }; publicDatabase.AddOperation(queryOperation); }
public void LoadBatch() { lock (locker) { // Quickly returns if another loadNextBatch is running or we have the oldest post if (isLoadingBatch || haveOldestPost) { return; } else { isLoadingBatch = true; } } CKQueryOperation queryOp = CreateLoadBatchOperation(); fetchRecordQueue.AddOperation(queryOp); }
CKQueryOperation CreateLoadBatchOperation() { CKQueryOperation queryOp = null; if (postCursor != null) { // If we have a cursor, go ahead and just continue from where we left off queryOp = new CKQueryOperation (postCursor); } else { CKQuery postQuery = CreatePostQuery (); queryOp = new CKQueryOperation (postQuery); } downloadingBatchStorage.Clear (); // This query should only fetch so many records and only retrieve the information we need queryOp.ResultsLimit = updateBy; queryOp.DesiredKeys = desiredKeys; queryOp.RecordFetched = OnPostFetched; queryOp.Completed = OnPostLoadingCompleted; queryOp.Database = PublicDB; return queryOp; }
// Called when users pulls to refresh // This adds new items to the beginning of the table public void LoadNewPosts(Post post = null) { // If we don't have any posts on our table yet, // fetch the first batch instead (we make assumptions in this method that we have other posts to compare to) if (TryLoadFirstBatch()) { return; } // We want to strip all posts we have that haven't been seen on the server yet from tableview (order isn't guaranteed) var loc = PostCells.IndexOf(lastPostSeenOnServer); List <Post> newPosts = PostCells.Take(loc).ToList(); PostCells.RemoveRange(0, loc); // If we had a post passed in and it matches our tags, we should put that in the array too if (MatchesTags(post)) { newPosts.Add(post); } else { post = null; } // The last record we see will be the most recent we see on the server, we'll set the property to this in the completion handler Post lastRecordInOperation = null; var postQuery = CreateLoadNewPostQuery(); var queryOp = new CKQueryOperation(postQuery); queryOp.DesiredKeys = desiredKeys; queryOp.RecordFetched = (CKRecord record) => OnLoadNewPostFetchRecord(record, newPosts, ref lastRecordInOperation); queryOp.Completed = (CKQueryCursor cursor, NSError operationError) => OnLoadNewPostComplted(cursor, operationError, newPosts, lastPostSeenOnServer, post); queryOp.Database = PublicDB; fetchRecordQueue.AddOperation(queryOp); }
void LoadImages() { // If we're already loading a set of images or there are no images left to load, just return lock (locker) { if (isLoadingBatch || firstThumbnailLoaded) { return; } else { isLoadingBatch = true; } } // If we have a cursor, continue where we left off, otherwise set up new query CKQueryOperation queryOp = null; if (imageCursor != null) { queryOp = new CKQueryOperation(imageCursor); } else { CKQuery thumbnailQuery = new CKQuery(Image.RecordType, NSPredicate.FromValue(true)); thumbnailQuery.SortDescriptors = Utils.CreateCreationDateDescriptor(ascending: false); queryOp = new CKQueryOperation(thumbnailQuery); } // We only want to download the thumbnails, not the full image queryOp.DesiredKeys = new string[] { Image.ThumbnailKey }; queryOp.ResultsLimit = UpdateBy; queryOp.RecordFetched = (CKRecord record) => { ImageCollection.AddImageFromRecord(record); InvokeOnMainThread(() => { loadingImages.StopAnimating(); ImageCollection.ReloadData(); }); }; queryOp.Completed = (CKQueryCursor cursor, NSError error) => { Error errorResponse = HandleError(error); if (errorResponse == Error.Success) { imageCursor = cursor; isLoadingBatch = false; if (cursor == null) { firstThumbnailLoaded = true; // If cursor is nil, lock this method indefinitely (all images have been loaded) } } else if (errorResponse == Error.Retry) { // If there's no specific number of seconds we're told to wait, default to 3 Utils.Retry(() => { // Resets so we can load images again and then goes to load isLoadingBatch = false; LoadImages(); }, error); } else if (errorResponse == Error.Ignore) { // If we get an ignore error they're not often recoverable. I'll leave loadImages locked indefinitely (this is up to the developer) Console.WriteLine("Error: {0}", error.Description); string errorTitle = "Error"; string dismissButton = "Okay"; string errorMessage = "We couldn't fetch one or more of the thumbnails"; UIAlertController alert = UIAlertController.Create(errorTitle, errorMessage, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create(dismissButton, UIAlertActionStyle.Cancel, null)); InvokeOnMainThread(() => PresentViewController(alert, true, null)); } }; PublicCloudDatabase.AddOperation(queryOp); }
public void QueryForRecords (string referenceRecordName, Action<List<CKRecord>> completionHandler) { var recordId = new CKRecordID (referenceRecordName); var parent = new CKReference (recordId, CKReferenceAction.None); var predicate = NSPredicate.FromFormat ("parent == %@", parent); var query = new CKQuery (ReferenceSubItemsRecordType, predicate) { SortDescriptors = new [] { new NSSortDescriptor ("creationDate", false) } }; var queryOperation = new CKQueryOperation (query) { DesiredKeys = new [] { NameField } }; var results = new List<CKRecord> (); queryOperation.RecordFetched = (record) => results.Add (record); queryOperation.Completed = (cursor, error) => { if (error != null) { Console.WriteLine ("An error occured: {0}", error.Description); return; } DispatchQueue.MainQueue.DispatchAsync (() => completionHandler (results)); }; publicDatabase.AddOperation (queryOperation); }
// Called when users pulls to refresh // This adds new items to the beginning of the table public void LoadNewPosts(Post post = null) { // If we don't have any posts on our table yet, // fetch the first batch instead (we make assumptions in this method that we have other posts to compare to) if (TryLoadFirstBatch ()) return; // We want to strip all posts we have that haven't been seen on the server yet from tableview (order isn't guaranteed) var loc = PostCells.IndexOf (lastPostSeenOnServer); List<Post> newPosts = PostCells.Take (loc).ToList (); PostCells.RemoveRange (0, loc); // If we had a post passed in and it matches our tags, we should put that in the array too if (MatchesTags (post)) newPosts.Add (post); else post = null; // The last record we see will be the most recent we see on the server, we'll set the property to this in the completion handler Post lastRecordInOperation = null; var postQuery = CreateLoadNewPostQuery (); var queryOp = new CKQueryOperation (postQuery); queryOp.DesiredKeys = desiredKeys; queryOp.RecordFetched = (CKRecord record) => OnLoadNewPostFetchRecord (record, newPosts, ref lastRecordInOperation); queryOp.Completed = (CKQueryCursor cursor, NSError operationError) => OnLoadNewPostComplted (cursor, operationError, newPosts, lastPostSeenOnServer, post); queryOp.Database = PublicDB; fetchRecordQueue.AddOperation (queryOp); }
public void SetUp() { TestRuntime.AssertXcodeVersion(6, 0); q = new CKQuery("Foo", NSPredicate.FromFormat("email = '@xamarin'")); op = new CKQueryOperation(q); }
void LoadImages() { // If we're already loading a set of images or there are no images left to load, just return lock (locker) { if (isLoadingBatch || firstThumbnailLoaded) return; else isLoadingBatch = true; } // If we have a cursor, continue where we left off, otherwise set up new query CKQueryOperation queryOp = null; if (imageCursor != null) { queryOp = new CKQueryOperation (imageCursor); } else { CKQuery thumbnailQuery = new CKQuery (Image.RecordType, NSPredicate.FromValue (true)); thumbnailQuery.SortDescriptors = Utils.CreateCreationDateDescriptor (ascending: false); queryOp = new CKQueryOperation (thumbnailQuery); } // We only want to download the thumbnails, not the full image queryOp.DesiredKeys = new string[] { Image.ThumbnailKey }; queryOp.ResultsLimit = UpdateBy; queryOp.RecordFetched = (CKRecord record) => { ImageCollection.AddImageFromRecord (record); InvokeOnMainThread (() => { loadingImages.StopAnimating (); ImageCollection.ReloadData (); }); }; queryOp.Completed = (CKQueryCursor cursor, NSError error) => { Error errorResponse = HandleError (error); if (errorResponse == Error.Success) { imageCursor = cursor; isLoadingBatch = false; if (cursor == null) firstThumbnailLoaded = true; // If cursor is nil, lock this method indefinitely (all images have been loaded) } else if (errorResponse == Error.Retry) { // If there's no specific number of seconds we're told to wait, default to 3 Utils.Retry (() => { // Resets so we can load images again and then goes to load isLoadingBatch = false; LoadImages (); }, error); } else if (errorResponse == Error.Ignore) { // If we get an ignore error they're not often recoverable. I'll leave loadImages locked indefinitely (this is up to the developer) Console.WriteLine ("Error: {0}", error.Description); string errorTitle = "Error"; string dismissButton = "Okay"; string errorMessage = "We couldn't fetch one or more of the thumbnails"; UIAlertController alert = UIAlertController.Create (errorTitle, errorMessage, UIAlertControllerStyle.Alert); alert.AddAction (UIAlertAction.Create (dismissButton, UIAlertActionStyle.Cancel, null)); InvokeOnMainThread (() => PresentViewController (alert, true, null)); } }; PublicCloudDatabase.AddOperation (queryOp); }