UA-17470720-3

Jump to content


Photo
- - - - -

Can SCWebService handle child objects?


  • Please log in to reply
4 replies to this topic

#1 petmod

petmod

    Starting Out

  • STV 3.0 Pro
  • Pip
  • 3 posts
Reputation: 1
Good

Posted 22 May 2013 - 08:34 PM

Hello,

 

Very new to sensible cocoa, am attempting to use the integrated web service loading to populate a table view.

 

Example JSON from web service:

{ "results" : [ { "animals" : [ {  
              "AnimalID" : "611",
              "DOB" : 40968,
              "Genotype" : "CONEDKI/CONEDKI,WT",
              "LineName" : "CONJ20",
              "Sex" : "M",
              "Status" : "D",
              "UAI" : 1623848
            },
            { "AnimalID" : "612",
              "DOB" : 40968,
              "Genotype" : "CONEDKI/CONEDKI,WT",
              "LineName" : "CONJ20",
              "Sex" : "M",
              "Status" : "D",
              "UAI" : 1623849
            }
          ],
        "container" : { "AnimalCount" : 2,
            "ContainerID" : 424799,
            "LineName" : "CONJ20",
            "OwnerName" : "xyyyde eeer",
            "ZoneName" : "xe(3.49)"
          }
      } ] }

 

As you can see the structure of a result contains two objects/classes each with attributes. The code below is what I have attempted to use:

 

self.title = @"Scan Container";
    SCWebServiceDefinition *serviceDef = [SCWebServiceDefinition definitionWithBaseURL:@"https://the-url.com" fetchObjectsAPI:@"getContainer.php" resultsKeyName:@"results" resultsDictionaryKeyNamesString:@"container;animals" ];
    [serviceDef.fetchObjectsParameters setValue:@"xxx" forKey:@"token"];
    [serviceDef.fetchObjectsParameters setValue:@"xxx" forKey:@"containerID"];
    
    self.tableViewModel.enablePullToRefresh = TRUE;
    SCPropertyDefinition *containerDef = [serviceDef propertyDefinitionWithName:@"container"];
    containerDef.title = @"Container Details";
    containerDef.type = SCPropertyTypeArrayOfObjects;
    containerDef.attributes = [SCArrayOfObjectsAttributes attributesWithObjectDefinition:serviceDef allowAddingItems:NO allowDeletingItems:NO allowMovingItems:NO];
    
    SCArrayOfObjectsSection *viewSections =[SCArrayOfObjectsSection sectionWithHeaderTitle:nil webServiceDefinition:serviceDef batchSize:7];
    
    viewSections.itemsAccessoryType = UITableViewCellAccessoryNone;
    [self.tableViewModel addSection:viewSections];

 

Running the app; after selecting a result, then selecting 'Container Details' shows a single cell with the raw json as the contents. I think the problem is when I set the SCPropertyDefinition.attributes value I am using attributesWithObjectDefinition but should be something like attributesWithWebServiceDefinition, which doesn't not exist.

 

Anyone accomplished what I am attempting, or does STV simply not support this? Any help greatly appreciated.

 

Cheers

-Peter

 



#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 22 May 2013 - 08:45 PM

Hi Peter and welcome to STV! :)

 

Your code is mostly correct, except in the following line:

 

containerDef.attributes = [SCArrayOfObjectsAttributes attributesWithObjectDefinition:serviceDef allowAddingItems:NO allowDeletingItems:NO allowMovingItems:NO];

 

STV is expecting here an object definition for 'container', but you gave serviceDef instead. Since 'container' has been parsed as an NSMutableDictionary, you should create an SCDictionaryDefinition to let STV know how 'container' is defined. Your code should look something like this:

 

...
SCDictionaryDefinition *containerDefinition = [SCDictionaryDefinition definitionWithDictionaryKeyNamesString:@"LineName;OwnerName;ZoneName"];
containerDef.type = SCPropertyTypeArrayOfObjects;
containerDef.attributes = [SCArrayOfObjectsAttributes attributesWithObjectDefinition:containerDefinition allowAddingItems:NO allowDeletingItems:NO allowMovingItems:NO];
...

 

Hope this helps.



#3 petmod

petmod

    Starting Out

  • STV 3.0 Pro
  • Pip
  • 3 posts
Reputation: 1
Good

Posted 22 May 2013 - 09:24 PM

Hi Tarek

 

Thanks for your quick reply :)

 

Setting the type to SCPropertyTypeArrayOfObjects worked for the 'animals' component of the json. However as there is only one 'container' it is not an array of objects, rather only a single object. I tried using SCPropertyTypeObject however this caused the app to crash.

 

Could you point me in the right direction.

 

Thanks

-Peter



#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 22 May 2013 - 09:36 PM

Please keep in mind that you should always use an attribute that's compatible with the property type. Since you changed your property type to SCPropertyTypeObject, you should in turn set the attributes property to SCObjectAttributes, instead of SCArrayOfObjectsAttributes. You should find much more details on that in our bundled STV eBook.

 

Good luck!



#5 petmod

petmod

    Starting Out

  • STV 3.0 Pro
  • Pip
  • 3 posts
Reputation: 1
Good

Posted 22 May 2013 - 09:39 PM

Excellent, thanks.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users