How to design Object Graph/Model?

前提是你 提交于 2020-01-07 06:37:07

问题


JSON As Dictionary 
 {
    headers =     (
                {
            backGroundImageUrl = "";
            dataField = Name;
            headerText = Name;
            id = Name;
            itemRenderer = "";
            toolTip = "";
            width = 60;
        },
                {
            backGroundImageUrl = "";
            dataField = BidPrice;
            headerText = Bid;
            id = BidPrice;
            itemRenderer = "";
            toolTip = "Bid Price";
            width = 30;
        });
 values =     (
                {
            assetCellValueLst =             {
                AskColorCode = "#B8D1ED";
                AskPrice = "102.20";
                BidColorCode = "#B8D1ED";
                BidPrice = "102.00";
                Name = "AR Bonar 11";
                PECSAsk = 569;
                PECSChg = "(31)";
                PECSChgColorCode = "#000000";
                PriceChg = "0.00";
                PriceChgColorCode = "#000000";
                SOLAsk = 604;
                SSPAsk = 677;
                SSPChgDay = "+3";
                SSPChgDayColorCode = "#000000";
                YTMAsk = "6.97";
                assetGroupName = Argentina;
                assetId = ARBONAR11;
                iconPath = "images/flag_Argentina.gif";
                updated = false;
            };
            assetId = ARBONAR11;
        },
                {
            assetCellValueLst =             {
                AskColorCode = "#53840f";
                AskPrice = "84.00";
                BidColorCode = "#53840f";
                BidPrice = "83.75";
                Name = "AR Bod 15";
                PECSAsk = 945;
                PECSChg = 14;
                PECSChgColorCode = "#000000";
                PriceChg = "-0.10";
                PriceChgColorCode = "#53840F";
                SOLAsk = 985;
                SSPAsk = 1007;
                SSPChgDay = "+7";
                SSPChgDayColorCode = "#000000";
                YTMAsk = "11.74";
                assetGroupName = Argentina;
                assetId = ARBON15;
                iconPath = "images/flag_Argentina.gif";
                updated = false;
            };
            assetId = ARBON15;
        });

The above JSON has more headers and values shortened here for clarity. The assestCellValueLst Dictionary can have more or less key-value pair depending on business logic. Same is true for headers dictionary. How should I create a Object Model in Xcode to use it with Core Data Managed Objects ? for a non - persistent store or no store at all


回答1:


Simplistically, you would just create an entity for each type of dictionary you wanted to persist and have an attribute for each key in the dictionary.

In this case it looks like you would have two entities: Header and Asset. If the headers have a one-to-relationship with assets you might have (pseudocode) entities like this:

Header{
    backGroundImageUrl = string;
    dataField = string;
    headerText = string;
    id = string;
    itemRenderer = ?;
    toolTip = ?
    width = integer;
    asset<--(required,cascade)-->Asset.header
}

Asset{
    assetId = string;
    askColorCode = string;
    askPrice = float;
    bidColorCode = string;
    bidPrice = float;
    //...etc
    header<--(required,nullify)-->Header.asset
}

The basic idea is that the model, well, "models" whatever real world object, event, condition or information you want to persist. In this case, the JSON is providing nice, neat dictionaries with all the data already organized and labeled for you.




回答2:



+(NSArray *)managedObjectsFromJSONDictionary:(NSDictionary *)dictionary withManagedObjectContext:(NSManagedObjectContext *)moc
{
    NSMutableArray *finalArrayOfArrays = [[NSMutableArray alloc] init];
    NSArray *allKeys = [dictionary allKeys];
    for(NSString *keyName in allKeys)
    {
        id structureArray = [dictionary valueForKey:keyName];
        if([structureArray isKindOfClass:[NSArray class]])
        {
            NSMutableArray *objectArray = [[NSMutableArray alloc] init];
            for(NSDictionary *structureDictionary in (NSArray *)structureArray)
            {
                [objectArray addObject:[JSONtoManagedObject managedObjectFromDictionary:structureDictionary withManagedObjectContext:moc forEntity:keyName]];
            }
            [finalArrayOfArrays addObject:objectArray];
            [objectArray release];
        }
    }
    return [finalArrayOfArrays autorelease];
}
+(NSManagedObject *)managedObjectFromDictionary:(NSDictionary *)dictionary withManagedObjectContext:(NSManagedObjectContext *)moc forEntity:(NSString *)entityName
{
    NSManagedObject *managedObject;
    NSArray *arrayOfKeys = [dictionary allKeys];
    for(NSString *name in arrayOfKeys)
    {
        id obj = [dictionary objectForKey:name];
            managedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:moc];
                if (![obj isKindOfClass:[NSDictionary class]])
                {
                    if([name isEqualToString:@"id"])
                        [managedObject setValue:obj forKey:@"uniqueID"];
                    else
                        [managedObject setValue:obj forKey:name];
                }
                else 
                {
                    NSDictionary *relationshipDictionary = [[managedObject entity] relationshipsByName];
                    for(NSString *relationshipName in [relationshipDictionary allKeys])
                    {
                        NSRelationshipDescription *relationshipDescription = [relationshipDictionary objectForKey:relationshipName];
                        if(![relationshipDescription isToMany] && [relationshipName isEqualToString:name])
                        {
                            NSManagedObject *childObject = [JSONtoManagedObject managedObjectFromDictionary:obj withManagedObjectContext:moc forEntity:relationshipName];
                            [managedObject setValue:childObject forKey:relationshipName];
                            continue;
                        }
                        NSMutableSet *relationshipSet = [managedObject mutableSetValueForKey:relationshipName];
                        NSArray *relationshipArray = [obj valueForKey:relationshipName];
                        for(NSDictionary *insideDictoinary in relationshipArray)
                        {
                            NSManagedObject *childObject = [JSONtoManagedObject managedObjectFromDictionary:insideDictoinary withManagedObjectContext:moc forEntity:relationshipName];
                            [relationshipSet addObject:childObject];
                        }
                    }
                }//end else
        } // for
    return managedObject;
}


来源:https://stackoverflow.com/questions/3322191/how-to-design-object-graph-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!