Save Array of feature in documentdirectory

3707
3
11-10-2015 10:20 AM
SotlarStéphane
New Contributor II

Hi,

i have a NSMutableArray whit Feature and i want to write the NSMutableArray to my NSDocumentDirectory.

my code :

NSLog(@"ARRAY CARTE SCOLAIRE :%@", arrayCarteScolaire);

        NSArray *arrayCarteSauvegarde = [[NSArray alloc] initWithArray:arrayCarteScolaire];

        NSLog(@"ARRAY SAUVEGARDE :%@", arrayCarteSauvegarde);

        NSString* imageFilename = [NSString stringWithFormat:@"SAUVEGARDE CARTE %@", anneeChoisiTab];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSString* imageFilePathname = [documentsDirectory stringByAppendingPathComponent:imageFilename];

        //on fait la verif pour voir s'il y a les images du bundle dans le DocumentDirectory

        if ([[NSFileManager defaultManager] fileExistsAtPath:imageFilePathname]) {

            NSLog(@"Fichier %@ déjà présent dans le dossier document - Voir pour la modification", imageFilename);

            //on efface et on copie

            NSError *error;

            BOOL success = [[NSFileManager defaultManager] removeItemAtPath:imageFilePathname error:&error];

            if (success) {

                NSLog(@"Le fichier %@ a bien été effacé", imageFilename);

                //UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:@"Congratulation:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];

                //[removeSuccessFulAlert show];

            }

            else

            {

                NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);

            }

            //on copie le fichier

            NSError *error2;

            BOOL yes = [arrayCarteSauvegarde writeToFile:imageFilePathname atomically:YES];

            if (yes) {

                NSLog(@"Fichier %@ créer", imageFilename);

            }

            else {

                NSLog(@"Could not create file -:%@ ",[error2 localizedDescription]);

            }

        }

        else {

            NSLog(@"Fichier %@ non présent dans le dossier document", imageFilename);

            //on va copier le fichier normalement

            NSError *error2;

            BOOL yes = [arrayCarteSauvegarde writeToFile:imageFilePathname atomically:YES];

            //BOOL yes = [NSKeyedArchiver archiveRootObject:arrayCarteSauvegarde toFile:imageFilePathname];

            if (yes) {

                NSLog(@"Fichier %@ créer", imageFilename);

            }

            else {

                NSLog(@"Could not create file -:%@ ",[error2 localizedDescription]);

            }

        }

The file is not create in the document directory but no crash.

With this code :

else {

            NSLog(@"Fichier %@ non présent dans le dossier document", imageFilename);

            //on va copier le fichier normalement

            NSError *error2;

            //BOOL yes = [arrayCarteSauvegarde writeToFile:imageFilePathname atomically:YES];

            BOOL yes = [NSKeyedArchiver archiveRootObject:arrayCarteSauvegarde toFile:imageFilePathname];

            if (yes) {

                NSLog(@"Fichier %@ créer", imageFilename);

            }

            else {

                NSLog(@"Could not create file -:%@ ",[error2 localizedDescription]);

            }

        }

The app crash with this error :

2015-11-10 13:42:59.243 Ecoles Colleges 06 Antibes[211:11878] -[AGSGDBFeature encodeWithCoder:]: unrecognized selector sent to instance 0x1742a05a0

2015-11-10 13:42:59.245 Ecoles Colleges 06 Antibes[211:11878] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AGSGDBFeature encodeWithCoder:]: unrecognized selector sent to instance 0x1742a05a0'

Thanks for regards.

Stounfr

Tags (2)
0 Kudos
3 Replies
KristofferStenersen
Occasional Contributor

Hi, the AGSGDBFeature cannot be saved to disk like this, as it does not subclass NSCoder.

I guess a reasonable question is why you want to persist the AGSGDBFeatures yourself - why not use the AGSGDBFeatureTable´s saveFeature?

0 Kudos
SotlarStéphane
New Contributor II

Hi,

I want to copy AGSGDBFeature array in the device because i dont what modify the geodatabase.

When the user quit the app and reload it, i want that the app charge the geodatabase not modified and if the user want charge the modified Feature the app take the AGSGDBFeature.

Stounfr

0 Kudos
SotlarStéphane
New Contributor II

the how to :

for save

NSMutableArray *arrayCarteSauvegarde = [[NSMutableArray alloc] init];

        for (int i=0; i <[arrayCarteScolaire count]; i++) {

            AGSGDBFeature *feat = [arrayCarteScolaire objectAtIndex:i];

            NSDictionary *dicFeat = feat.allAttributes;

            NSDictionary *dicFeat2 = [dicFeat ags_encodeJSON];

            NSString *stringJSON = [dicFeat2 ags_JSONRepresentation];

            NSLog(@"STRING FEATURE : %@", stringJSON);

            [arrayCarteSauvegarde addObject:stringJSON];

            NSLog(@"%@", feat.geometry);

            //NSLog(@"JSON : %@", dicFeat2);

        }

BOOL yes = [arrayCarteSauvegarde writeToFile:imageFilePathname atomically:YES];

            //BOOL yes = [NSKeyedArchiver archiveRootObject:arrayCarteSauvegarde toFile:imageFilePathname];

            //BOOL yes = [dataArray writeToFile:imageFilePathname atomically:YES];

            if (yes) {

                NSLog(@"Fichier %@ créer", imageFilename);

            }

            else {

                NSLog(@"Could not create file -:%@ ",[error2 localizedDescription]);

            }

for load

retourCarte = [NSArray arrayWithContentsOfFile:imageFilePathname];

for (int i=0; i<[retourCarte count]; i++) {

            //Bon on récupère les infos

            //NSDictionary *dicFeat = [retourCarte objectAtIndex:i];

            NSString *testJSON = [retourCarte objectAtIndex:i];

            NSLog(@"TEST JSON SHAPE : %@", testJSON);

            NSDictionary *dicFeat = [testJSON ags_JSONValue];

            NSLog(@"DICO JSON : %@", dicFeat);

            NSDictionary *geoDict = [dicFeat valueForKey:@"shape"];

            NSLog(@"SHAPE : %@", geoDict);

            AGSGeometry *geom = [[AGSGeometry alloc] initWithJSON:geoDict];

            NSLog(@"GEOMETRY : %@", geom);

            AGSMutablePolygon *testTest = [[AGSMutablePolygon alloc] initWithJSON:geoDict];

            NSLog(@"POLYGON : %@", testTest);

            /*NSDictionary *dicFeat2 = [testJSON ags_JSONValue];

            //NSLog(@"DIC JASON TRAITER : %@", dicFeat2);

            feat = [retourCarte objectAtIndex:i];

            //NSLog(@"FEAT : %@", feat);

            feat2.geometry = geom;

            //NSLog(@"FEAT 2 : %@", feat2);

            //[arrayCarteScolaire addObject:feat];*/

            //AGSGraphic *test = [[AGSGraphic alloc] initWithJSON:geoDict];

            AGSGraphic *test = [[AGSGraphic alloc] initWithGeometry:testTest symbol:nil attributes:dicFeat];

            NSLog(@"AGSGraphic = %@", test);

            [arrayCarteScolaire addObject:test];

        }

Stounfr