Creating classes from the CoreData model

I am trying to develop a couple of applications in C #, one iOS, one Mac, which use the same CoreData database. In fact, the desktop application populates it, and then it is distributed as part of the iOS application.

I can use Xcode to create a file .xcdatamodeldthat describes how I want the database to look. I can use momcto compile to a file .momd. I can include the file .momfrom this in my Mono project and upload it to NSManagedObjectModel, from which I can access all the properties of various objects.

What I still do not understand how to do is to create a class object from the database, and not access the table properties. Any suggestions?


To clarify: I want to be able to create a table / class in Xcode, name it Person. I give him two fields: Nameand Phone. I want to be able to run code similar to this in Mono:

using (var context = new NSManagedObjectContext())
{
  var me = context.Person.GetByID(1);
  me.Name = "Bobson";
  context.Save();
}

Obviously, the features of getting from the database and saving it will be different, but there is an essence.

0
source share
3 answers

As with the @ t9mike comment (which I will accept if it ever answers), I refused to use CoreData in favor of a more cross-platform approach. I ended up using Vici CoolStorage for ORM, although I had to embed the source in my project to make it work.

0
source

CoreData MonoTouch. , momc.exe. .

(, ), "BundleResource" (, ). UIManagedDocument CoreData mom, , iCloud.

. script, xcdatamodel #. , .

public partial class Photographer : NSManagedObject
{
    public static NSString NameKey = (NSString) "name";
    public static NSString PhotosKey = (NSString) "photos";

    public Photographer(IntPtr handle) : base(handle)
    {
    }

    public string Name
    {
        get { return (NSString) Runtime.GetNSObject(ValueForKey(NameKey)); }
        set { SetValueForKey(value, NameKey); }
    }

    public NSSet Photos
    {
        get { return (NSSet) Runtime.GetNSObject(ValueForKey(PhotosKey)); }
        set { SetValueForKey(value, PhotosKey); }
    }

    void SetValueForKey(string value, NSString key)
    {
        base.SetValueForKey((NSString)(value ?? ""), key);
    }

    public static Photographer InsertNewObject(NSManagedObjectContext context)
    {
        return (Photographer) NSEntityDescription.InsertNewObjectForEntityForName("Photographer", context);
    }

    public static Photographer WithName(string name, NSManagedObjectContext context)
    {
        Photographer photographer = null;

        // This is just like Photo(Flickr) method.  Look there for commentary.

        if (name.Length > 0)
        {
            var request = new NSFetchRequest("Photographer")
            {
                SortDescriptors = new[] {new NSSortDescriptor("name", true, new Selector("localizedCaseInsensitiveCompare:"))},
                Predicate =  NSPredicate.FromFormat("name = %@", new NSObject[] {(NSString) name})
            };

            NSError error;
            var matches = context.ExecuteFetchRequest(request, out error);

            if (matches == null || matches.Length > 1)
            {
                // handle error
            }
            else if (matches.Length == 0)
            {
                photographer = InsertNewObject(context);
                photographer.Name = name;
            }
            else
            {
                photographer = (Photographer) matches.First();
            }
        }

        return photographer;
    }
}

public partial class Photo : NSManagedObject
{
    public static NSString ImageUrlKey = (NSString) "imageURL";
    public static NSString TitleKey = (NSString) "title";
    public static NSString UniqueKey = (NSString) "unique";
    public static NSString SubtitleKey = (NSString) "subtitle";
    public static NSString WhoTookKey = (NSString) "whoTook";

    public Photo (IntPtr handle) : base (handle)
    {
    }

    public string ImageUrl  {
        get { return (NSString)Runtime.GetNSObject(ValueForKey(ImageUrlKey)); }
        set { SetValueForKey(value, ImageUrlKey); }
    }

    public string Subtitle  {
        get { return (NSString)Runtime.GetNSObject(ValueForKey(SubtitleKey)); }
        set { SetValueForKey(value, SubtitleKey); }
    }

    public string Title  {
        get { return (NSString)Runtime.GetNSObject(ValueForKey(TitleKey)); }
        set { SetValueForKey(value, TitleKey); }
    }

    public string Unique  {
        get { return (NSString)Runtime.GetNSObject(ValueForKey(UniqueKey)); }
        set { SetValueForKey(value, UniqueKey); }

    }

    public Photographer WhoTook  {
        get { return (Photographer)Runtime.GetNSObject(ValueForKey(WhoTookKey)); }
        set { SetValueForKey(value, WhoTookKey); }
    }

void SetValueForKey(string value, NSString key)
{
    base.SetValueForKey((NSString) (value??""), key);
}

    public static Photo InsertNewObject(NSManagedObjectContext context)
    {
        return (Photo) NSEntityDescription.InsertNewObjectForEntityForName("Photo", context);
    }

    public UIImage Image
    {
        get {
            if (string.IsNullOrEmpty(ImageUrl))
                return null;

            var imageData = NSData.FromUrl(new NSUrl(ImageUrl));
            if (imageData == null)
                return null;
            return new UIImage(imageData);
        }
    }
+1

, , :

public partial class Item : NSManagedObject
{
   internal Item(string entityName)
      : base(NSEntityDescription)m_managedObjectModel.EntitiesByName.ObjectForKey(new NSString(entityName)), m_managedObjectContext)

entityName - MOM.

0

All Articles