Partage
  • Partager sur Facebook
  • Partager sur Twitter

[iOS] RestKit et mappage relation

Sujet résolu
    2 août 2012 à 11:35:09

    Bonjour, voilà mon problème j'ai 2 objets reliés. Data et CompositeCard. Je souhaite récupérer la liste de des Data grâce à l'id donné d'une CompositeCard. Pour cela j'ai un web service Restful qui fonctionne.

    J'ai passé des jours sur le net à chercher et à essayer des codes mais rien de fonctionne.
    Alors j'espère que quelqu'un pourra m'aider.
    Voici toutes mes classes.

    ViewControllerData.h
    #import <UIKit/UIKit.h>
    
    @interface ViewControllerData : UIViewController<UIAlertViewDelegate,RKObjectLoaderDelegate>
    {
        UITableView* _tableView;
        NSArray* _data;
    }
    @end
    


    ViewControllerData.m
    #import "ViewControllerData.h"
    #import "Data.h"
    #import "CompositeCard.h"
    
    @interface ViewControllerData ()
    
    @end
    
    @implementation ViewControllerData
    
    
    - (void)loadTimeline {
        // Charger le modèle via RestKit
        RKObjectManager* objectManager = [RKObjectManager sharedManager];
        objectManager.client.baseURL = [RKURL URLWithString:@"http://localhost:8080/CulturalNetworksMuseumServer/resources"];
        [objectManager loadObjectsAtResourcePath:@"/compositecards/1/artifactcards" delegate:self];
    }
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
    	
        RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace);
        RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
        
        // Initialisation de RestKit
        RKObjectManager* objectManager = [RKObjectManager managerWithBaseURLString:@"http://localhost:8080/CulturalNetworksMuseumServer/resources"];
        
        // Active la gestion automatique de l'indicateur de l'activité réseau
        objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
        
        //Configuration du mappage de l'objet CompositeCard
        RKObjectMapping* compositeMapping = [RKObjectMapping mappingForClass:[CompositeCard class]];
    //    [compositeMapping mapKeyPath:@"id" toAttribute:@"cc_id"];
    //    [compositeMapping mapKeyPath:@"name" toAttribute:@"cc_name"];  
        [compositeMapping mapAttributes:@"id", @"name", nil];
        
        //[[objectManager mappingProvider] setObjectMapping:compositeMapping forKeyPath:@"/compositecards"];
        
        //Configuration du mappage de l'objet Data
        RKObjectMapping * dataMapping = [RKObjectMapping mappingForClass:[Data class]];
        [dataMapping mapKeyPath:@"id" toAttribute:@"ac_id"];
        [dataMapping mapKeyPath:@"file" toAttribute:@"ac_file"];
        [dataMapping mapKeyPath:@"typefile" toAttribute:@"ac_typefile"];
        [dataMapping mapKeyPath:@"author" toAttribute:@"ac_author"];
        [dataMapping mapKeyPath:@"title_topic" toAttribute:@"ac_title_topic"];
        [dataMapping mapKeyPath:@"year" toAttribute:@"ac_year"];
        [dataMapping mapKeyPath:@"technical" toAttribute:@"ac_technical"];
        [dataMapping mapKeyPath:@"support_media" toAttribute:@"ac_support_media"];
        [dataMapping mapKeyPath:@"format" toAttribute:@"ac_format"];
        [dataMapping mapKeyPath:@"owner" toAttribute:@"ac_owner"];
        [dataMapping mapKeyPath:@"collection" toAttribute:@"ac_collection"];
        [dataMapping mapKeyPath:@"descritpion_remarks" toAttribute:@"ac_descritpion_remarks"];
        [dataMapping mapKeyPath:@"compositeCard" toRelationship:@"ac_compositeCard" withMapping:compositeMapping];
        
        
        // Permet de mapper l'objet artifactCard avec la source de ce dernier
        [objectManager.mappingProvider setObjectMapping:dataMapping forResourcePathPattern:@"/compositecards/1/artifactcards"];
        //[[objectManager mappingProvider] addObjectMapping:dataMapping];
        
        NSString * test = @"Mappage dans la View Controller";
        NSLog(@"%@",test);
        
        
    //    // mise en place d'une TableView
    //    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStylePlain];
    //    _tableView.dataSource = self;
    //    _tableView.delegate = self;
    //    _tableView.backgroundColor = [UIColor clearColor];
    //    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //    [self.view addSubview:_tableView];
        
        // appel de la fonction qui charge le modèle
        [self loadTimeline];
    
    }
    
    #pragma mark RKObjectLoaderDelegate methods
    - (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
    {
        NSLog(@"Loaded payload: %@", [response bodyAsString]);
    }  
    
    /**
     ** Permet de charger la liste des adresses provenant des services Rest sous forme de tableau
     ** Remplir le tableau _addresses (local) avec le tableau d'objects
     ** Mettre à jour la TableView
     **/
    - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
        NSLog(@"Load collection of ArtifactCards: %@", objects);
        _data = [NSArray arrayWithArray:objects];
        [_tableView reloadData];
    }
    
    - (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        NSLog(@"Hit error: %@", error);
    }
    
    //#pragma mark UITableViewDataSource methods
    //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //    return 1;
    //}
    //
    //- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    //    return [_data count];
    //}
    //
    //- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    NSString* reuseIdentifier = @"Data Cell";
    //    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    //    if (nil == cell) {
    //        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    //        cell.textLabel.numberOfLines = 0;
    //        cell.textLabel.backgroundColor = [UIColor clearColor];
    //    }
    //    Data *data = [_data objectAtIndex:indexPath.row];
    //    
    //    //[cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", address.ad_lat, address.ad_lon]];
    //    
    //    [cell.textLabel setText:[NSString stringWithFormat:@"%@", data.ac_title_topic]];
    //    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", data.ac_author]];
    //    
    //    return cell;
    //}
    
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    @end
    


    CompositeCard.h
    #import <Foundation/Foundation.h>
    
    @interface CompositeCard : NSObject{
        NSNumber* _cc_id;
        NSString* _cc_name;
    }
    
    @property (nonatomic, retain) NSNumber* cc_id;  
    @property (nonatomic, retain) NSString* cc_name;
    
    @end
    


    CompositeCard.m
    #import "CompositeCard.h"
    
    @implementation CompositeCard
    @synthesize cc_id = _cc_id;
    @synthesize cc_name = _cc_name;
    @end
    


    Data.h
    #import <Foundation/Foundation.h>
    #import "CompositeCard.h"
    
    @interface Data : NSObject{
    NSNumber* _ac_id;
    NSString* _ac_file;
    NSString* _ac_typefile;
    NSString* _ac_author;
    NSString* _ac_title_topic;
    NSString* _ac_year;
    NSString* _ac_technical;
    NSString* _ac_support_media;
    NSString* _ac_format;
    NSString* _ac_owner;
    NSString* _ac_collection;
    NSString* _ac_descritpion_remarks;
    CompositeCard* _ac_compositeCard;
    }
    
    @property (nonatomic, retain) NSNumber* ac_id;  
    @property (nonatomic, retain) NSString* ac_file;
    @property (nonatomic, retain) NSString* ac_typefile;
    @property (nonatomic, retain) NSString* ac_author;
    @property (nonatomic, retain) NSString* ac_title_topic;
    @property (nonatomic, retain) NSString* ac_year;
    @property (nonatomic, retain) NSString* ac_technical;
    @property (nonatomic, retain) NSString* ac_support_media;
    @property (nonatomic, retain) NSString* ac_format;
    @property (nonatomic, retain) NSString* ac_owner;
    @property (nonatomic, retain) NSString* ac_collection;
    @property (nonatomic, retain) NSString* ac_descritpion_remarks;
    @property (nonatomic, retain) CompositeCard* ac_compositeCard;
    
    @end
    


    Data.m
    #import "Data.h"
    
    @implementation Data
    
    @synthesize ac_id = _ac_id;
    @synthesize ac_file = _ac_file;
    @synthesize ac_typefile = _ac_typefile;
    @synthesize ac_author = _ac_author;
    @synthesize ac_title_topic = _ac_title_topic;
    @synthesize ac_year = _ac_year;
    @synthesize ac_technical = _ac_technical;
    @synthesize ac_support_media = _ac_support_media;
    @synthesize ac_format = _ac_format;
    @synthesize ac_owner = _ac_owner;
    @synthesize ac_collection = _ac_collection;
    @synthesize ac_descritpion_remarks = _ac_descritpion_remarks;
    @synthesize ac_compositeCard = _ac_compositeCard;
    
    @end
    


    J'espère que quelqu'un peut m'aider. Car je suis débutante dans ce milieu et j'ai essayé plein de chose sans succès...
    • Partager sur Facebook
    • Partager sur Twitter
      4 août 2012 à 22:32:53

      Tu bloques ou? Le WS te retourne quoi comme code d'erreur?
      • Partager sur Facebook
      • Partager sur Twitter

      [iOS] RestKit et mappage relation

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown