public void Bug2000_NSPersistentStoreCoordinator() { // from http://www.sgmunn.com/?p=1#comments NSAttributeDescription description = new NSAttributeDescription(); Assert.That(description.Handle, Is.Not.EqualTo(IntPtr.Zero), "NSAttributeDescription"); description.AttributeType = NSAttributeType.Integer32; description.Name = "SomeId"; description.Optional = false; NSEntityDescription entity = new NSEntityDescription(); Assert.That(entity.Handle, Is.Not.EqualTo(IntPtr.Zero), "NSEntityDescription"); entity.Name = "TestEntity"; entity.Properties = new NSPropertyDescription[1] { description }; NSManagedObjectModel model = new NSManagedObjectModel(); Assert.That(model.Handle, Is.Not.EqualTo(IntPtr.Zero), "NSManagedObjectModel"); model.Entities = new NSEntityDescription[1] { entity }; model.SetEntities(model.Entities, String.Empty); NSUrl url = new NSUrl("test.sqlite", false); // from http://bugzilla.xamarin.com/show_bug.cgi?id=2000 NSError error; var c = new NSPersistentStoreCoordinator(model); c.AddPersistentStoreWithType(NSPersistentStoreCoordinator.SQLiteStoreType, null, url, null, out error); Assert.True(Runtime.Arch == Arch.SIMULATOR ? error == null : error.Code == 512, "error"); }
public void Sections() { // https://bugzilla.xamarin.com/show_bug.cgi?id=13785 // use Caches directory so this works with tvOS on devices (as well as existing iOS devices) string applicationDocumentsDirectory = NSSearchPath.GetDirectories(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.User, true).LastOrDefault(); using (var ManagedObjectModel = new NSManagedObjectModel()) { { // create an entity description NSEntityDescription entity = new NSEntityDescription(); entity.Name = "Earthquake"; // create an attribute for the entity NSAttributeDescription date = new NSAttributeDescription(); date.AttributeType = NSAttributeType.Date; date.Name = "date"; date.Optional = false; NSAttributeDescription latitude = new NSAttributeDescription(); latitude.AttributeType = NSAttributeType.Double; latitude.Name = "latitude"; latitude.Optional = false; NSAttributeDescription location = new NSAttributeDescription(); location.AttributeType = NSAttributeType.String; location.Name = "location"; location.Optional = false; NSAttributeDescription longitude = new NSAttributeDescription(); longitude.AttributeType = NSAttributeType.Double; longitude.Name = "longitude"; longitude.Optional = false; NSAttributeDescription magnitude = new NSAttributeDescription(); magnitude.AttributeType = NSAttributeType.Float; magnitude.Name = "magnitude"; magnitude.Optional = false; NSAttributeDescription USGSWebLink = new NSAttributeDescription(); USGSWebLink.AttributeType = NSAttributeType.String; USGSWebLink.Name = "USGSWebLink"; USGSWebLink.Optional = false; // assign the properties to the entity entity.Properties = new NSPropertyDescription[] { date, latitude, location, longitude, magnitude, USGSWebLink }; // add the entity to the model, and then add a configuration that // contains the entities ManagedObjectModel.Entities = new NSEntityDescription[] { entity }; ManagedObjectModel.SetEntities(ManagedObjectModel.Entities, String.Empty); } using (var PersistentStoreCoordinator = new NSPersistentStoreCoordinator(ManagedObjectModel)) { { var storePath = applicationDocumentsDirectory + "/Earthquakes.sqlite"; var storeUrl = new NSUrl(storePath, false); NSError error; if (PersistentStoreCoordinator.AddPersistentStoreWithType(NSPersistentStoreCoordinator.SQLiteStoreType, null, storeUrl, null, out error) == null) { Assert.Fail("Unresolved error " + error + ", " + error.UserInfo); } } using (var ManagedObjectContext = new NSManagedObjectContext()) { ManagedObjectContext.PersistentStoreCoordinator = PersistentStoreCoordinator; // NSNotificationCenter.DefaultCenter.AddObserver ( // this, new MonoTouch.ObjCRuntime.Selector ("mergeChanges"), // "NSManagedObjectContextDidSaveNotification", null); NSFetchRequest fetchRequest = new NSFetchRequest(); NSEntityDescription entity = NSEntityDescription.EntityForName("Earthquake", ManagedObjectContext); fetchRequest.Entity = entity; NSSortDescriptor sortDescriptor = new NSSortDescriptor("date", false); fetchRequest.SortDescriptors = new [] { sortDescriptor }; NSFetchedResultsController fetchedResultsController = new NSFetchedResultsController( fetchRequest, ManagedObjectContext, null, null); NSError error; if (!fetchedResultsController.PerformFetch(out error)) { Assert.Fail("Unresolved error: " + error + ", " + error.UserInfo); } var sections = fetchedResultsController.Sections; Assert.That(sections [0].GetType().FullName, Is.StringEnding("CoreData.NSFetchedResultsSectionInfoWrapper"), "Wrapper"); } } } }