NSPersistentStoreCoordinator CreateCoordinator()
        {
            NSUrl url = StoreURL;

            if (url == null)
            {
                return(null);
            }

            var psc = new NSPersistentStoreCoordinator(ManagedObjectModel);

            var keys = new object[] { NSPersistentStoreCoordinator.MigratePersistentStoresAutomaticallyOption,
                                      NSPersistentStoreCoordinator.InferMappingModelAutomaticallyOption };
            var values = new object[] { true, true };

            NSDictionary options = NSDictionary.FromObjectsAndKeys(values, keys);

            NSError           error;
            NSPersistentStore store = psc.AddPersistentStoreWithType(NSPersistentStoreCoordinator.SQLiteStoreType, null, url, options, out error);

            if (store == null)
            {
                Console.WriteLine(error.Description);
                return(null);
            }

            return(psc);
        }
        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");
        }
        private void UpdateDatabaseWithPersistantStoreCoordinator(NSPersistentStoreCoordinator persistentStoreCoordinator,
                                                                  NSString storeType, NSUrl storeURL, NSUrl bundleUrl, bool DatabaseExists)
        {
            var          keys    = new NSObject[] { NSPersistentStoreCoordinator.MigratePersistentStoresAutomaticallyOption, NSPersistentStoreCoordinator.InferMappingModelAutomaticallyOption };
            var          objects = new NSObject[] { NSNumber.FromBoolean(true), NSNumber.FromBoolean(true) };
            NSDictionary options = NSDictionary.FromObjectsAndKeys(objects, keys);

            NSError error = null;

            if (!DatabaseExists)
            {
                persistentStoreCoordinator.AddPersistentStoreWithType(NSPersistentStoreCoordinator.SQLiteStoreType, null, bundleUrl, null, out error);

                if (error != null)
                {
                    throw new Exception(error.ToString());
                }
                error = null;
                persistentStoreCoordinator.MigratePersistentStore(persistentStoreCoordinator.PersistentStoreForUrl(bundleUrl), storeURL, null, storeType, out error);
                if (error != null)
                {
                    throw new Exception(error.ToString());
                }
            }
            else
            {
                error = null;
                persistentStoreCoordinator.AddPersistentStoreWithType(storeType, null, storeURL, null, out error);
                if (error != null)
                {
                    throw new Exception(error.ToString());
                }
            }
        }
		NSPersistentStoreCoordinator CreateCoordinator ()
		{
			NSUrl url = StoreURL;

			if (url == null)
				return null;

			var psc = new NSPersistentStoreCoordinator (ManagedObjectModel);

			var keys = new object[] { NSPersistentStoreCoordinator.MigratePersistentStoresAutomaticallyOption,
				NSPersistentStoreCoordinator.InferMappingModelAutomaticallyOption
			};
			var values = new object[] { true, true };

			NSDictionary options = NSDictionary.FromObjectsAndKeys (values, keys);

			NSError error;
			NSPersistentStore store = psc.AddPersistentStoreWithType (NSPersistentStoreCoordinator.SQLiteStoreType, null, url, options, out error);

			if (store == null) {
				Console.WriteLine (error.Description);
				return null;
			}

			return psc;
		}
		public APLParseOperation (NSData data, NSPersistentStoreCoordinator persistentStoreCoordinator)
		{
			dateFormatter = new NSDateFormatter () {
				DateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'",
				TimeZone = NSTimeZone.LocalTimeZone,
				Locale = NSLocale.FromLocaleIdentifier ("en_US_POSIX")
			};

			earthquakeData = data;
			sharedPSC = persistentStoreCoordinator;
		}
        public APLParseOperation(NSData data, NSPersistentStoreCoordinator persistentStoreCoordinator)
        {
            dateFormatter = new NSDateFormatter()
            {
                DateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'",
                TimeZone   = NSTimeZone.LocalTimeZone,
                Locale     = NSLocale.FromLocaleIdentifier("en_US_POSIX")
            };

            earthquakeData = data;
            sharedPSC      = persistentStoreCoordinator;
        }
        /// The managed object context for the view controller (which is bound to the persistent store coordinator for the application).
        NSManagedObjectContext CreatePrivateQueueContext(out NSError error)
        {
            // It uses the same store and model, but a new persistent store coordinator and context.
            var localCoordinator    = new NSPersistentStoreCoordinator(CoreDataStackManager.SharedManager.ManagedObjectModel);
            NSPersistentStore store = localCoordinator.AddPersistentStoreWithType(NSPersistentStoreCoordinator.SQLiteStoreType, null, CoreDataStackManager.SharedManager.StoreURL, null, out error);

            if (store == null)
            {
                return(null);
            }

            var context = new NSManagedObjectContext(NSManagedObjectContextConcurrencyType.PrivateQueue);

            context.PersistentStoreCoordinator = localCoordinator;
            context.UndoManager = null;
            return(context);
        }
Exemple #8
0
        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");
                    }
                }
            }
        }
		/// The managed object context for the view controller (which is bound to the persistent store coordinator for the application).
		NSManagedObjectContext CreatePrivateQueueContext (out NSError error)
		{
			// It uses the same store and model, but a new persistent store coordinator and context.
			var localCoordinator = new NSPersistentStoreCoordinator (CoreDataStackManager.SharedManager.ManagedObjectModel);
			NSPersistentStore store = localCoordinator.AddPersistentStoreWithType (NSPersistentStoreCoordinator.SQLiteStoreType, null, CoreDataStackManager.SharedManager.StoreURL, null, out error);

			if (store == null)
				return null;

			var context = new NSManagedObjectContext (NSManagedObjectContextConcurrencyType.PrivateQueue);
			context.PersistentStoreCoordinator = localCoordinator;
			context.UndoManager = null;
			return context;
		}