Je travaille sur une application iPhone en dessinant des superpositions sur une carte. Je veux enregistrer les tableaux qui contiennent les superpositions dans un NSMutableDictionary, mais seulement la dernière entrée que je sauvé des œuvres:Problème de mémoire NSMutableDictionary
// Get "Kurwege" from CoreData
NSError * error;
if(![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); // TODO
}
if(self.overlaysDictionary == nil) {
self.overlaysDictionary = [[NSMutableDictionary alloc] initWithCapacity:[fetchedResultsController.fetchedObjects count]];
}
NSString *path;
for (Kurweg *kurweg in fetchedResultsController.fetchedObjects) {
// Locate the path to the .kml file in the application's bundle
// and parse it with the KMLParser.
path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", [[kurweg valueForKey:@"kmlfile"] description]] ofType:@"kml"];
kml = [[KMLParser parseKMLAtPath:path] retain]; // PROBLEM: Executing this the second time seems to override my data stored in the dictionary
// Save overlays in dictionary
[self.overlaysDictionary setObject:[kml overlays] forKey:[NSString stringWithFormat:@"%@", [[kurweg valueForKey:@"kmlfile"] description]]];
}
// Draw overlays
NSArray *keys = [overlaysDictionary allKeys];
for (NSString *key in keys) {
// Add all of the MKOverlay objects parsed from the KML file to the map.
[map addOverlays:[overlaysDictionary objectForKey:key]];
}
Il est évident que c'est un problème de mémoire, mais malheureusement, je ne sais pas comment conserver les données stockées dans le dictionnaire.
Voici un extrait du code dans l'analyseur KML, pris à partir d'un exemple d'Apple:
- (NSArray *)overlays {
NSMutableArray *overlays = [[NSMutableArray alloc] init];
for (KMLPlacemark *placemark in _placemarks) {
id <MKOverlay> overlay = [placemark overlay];
if (overlay)
[overlays addObject:overlay];
}
return [overlays autorelease];
}
Merci à l'avance!
Où et comment la variable 'kml' est-elle définie? – dreamlax
La variable kml est définie dans le fichier d'en-tête: KMLParser * kml; Si possible, je ne veux pas enregistrer tout l'analyseur, mais uniquement la sortie (recouvrement NSArray) – sqeez3r
Au moins une fuite se produit lors de l'exécution de ce code. Est-ce que ceci pourrait être le problème? – sqeez3r