Many to many relation

后端 未结 2 1126
余生分开走
余生分开走 2021-01-07 08:33

I have two classes on Parse.com : Image & Data. In Data class, I am saving 3 fields Name,Mobile-number & Occupation. In Image class I\'m saving images.

I hav

相关标签:
2条回答
  • 2021-01-07 09:03

    I can't exactly tell you the code, but from database perspective, its not a good idea to have tables with many to many relationship. In order to resolve many <-> many relationships, you must break them as below:

    one -> many <- one

    For more info, read this: http://qisql.com/qisql_sqlite_many2many.html

    0 讨论(0)
  • 2021-01-07 09:26

    I'm not an Android developer, but I'll try to field this one. You have two classes, Data and Image. Each instance of Data can be associated to many Images. The piece of information that makes each instance of Data unique is the attribute named Mobile-Number.

    You have three options: 1. Parse.com Array 2. Parse.com Relation 3. An association class (as Waquas suggests).

    1. A Parse.com array is a good solution if an instance of Data is linked to fewer than about 50 images. When you associate an Image to a Data, use the Parse.com "add" method:
    aData.add("images", someImageObject);
    aData.saveInBackground();
    

    It is possible to associate a list of Images in one go:

    aData.addAll("images", Arrays.asList(image1, image2, image3));
    

    When you retrieve a Data instance from parse, the Image objects show up as an array of "pointers". To pull back the actual Image objects, use "fetch". For example of how to use fetch, look for this section of the Parse.com documentation:

    By default, when fetching an object, related ParseObjects are not fetched. These objects' values cannot be retrieved until they have been fetched like so:

    fetchedComment.getParseObject("post")
        .fetchIfNeededInBackground(new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
              String title = post.getString("title");
            }
        }); 
    
    1. A Parse.com relation is more scalable than the array solution. If a single Data is associated with hundreds or thousands of images, this is the correct solution. I have started switching my arrays to relations in my own code.
        ParseRelation relation = aData.getRelation("images");    
        relation.add(someImageObject);     
        aData.saveInBackground();
    
    1. An association class is a good solution if there is extra information about about the relationship between Data and Image-- for example, if a user can mark an image as "one of my favorites". To solve this problem, create a new class in Parse.com called ImageAssociation. The class has three attributes:

      • theDataObject (or just the Data object's Mobile Number)
      • theImageObject
      • isFavorite (true or false)

    I won't go into the mechanics of this solution. Follow Waquas' link in his answer for general information. See also the Parse.com documentation about "relations".

    0 讨论(0)
提交回复
热议问题