I am getting thread 1: signal SIGQUIT when running my app. But when I click on continue execution, app is resuming and completing the task. Not sure why I am getting this
NSArray *keyNamesForKAV = [[NSArray alloc]initWithObjects:#"aKAV",#"bKAV",#"cKAV",#"dKAV",#"eKAV",#"fKAV",#"gKAV",#"hKAV",#"iKAV",#"jKAV",#"kKAV",#"lKAV",#"mKAV",#"nKAV",#"oKAV",#"pKAV",#"qKAV",#"rKAV",#"sKAV",#"tKAV",#"uKAV",#"vKAV",#"wKAV",#"xKAV",#"yKAV",#"zKAV", nil];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
KAVTrie *kavTree = [defaults rm_customObjectForKey:#"zKAV"];
NSMutableArray *KAVArray = [[NSMutableArray alloc]init];
if (!kavTree){
for (int i = 0 ; i < 26; i++){
KAVTrie *trie = [[KAVTrie alloc]init];
KAVTrie *trieLoad = [[KAVTrie alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%c",[[keyNamesForKAV objectAtIndex:i] characterAtIndex:0]] ofType:#"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *array = [tempDict allKeys];
trie = [trieLoad storeTrieInArray:array];
[KAVArray addObject:trie];
}
NSLog(#"KAV objects created");
for (int i = 0 ; i < 26; i++){
[defaults rm_setCustomObject:[KAVArray objectAtIndex:i] forKey:[keyNamesForKAV objectAtIndex:i]];
[defaults synchronize];
}
NSLog(#"KAV objects loaded in user defaults");
}
I am getting error in the second for loop
I figured out. There is an issue with my KAV class file
Related
I have an NSMutableArray. The array is storing NSMutableDictionary. The dictionary object is storing NSUserDefault values. These are slider values previously selected by user. Now I want that, when user move these sliders then new slider values must be saved in NSUserDefaults.
I'm using this method to add a value in the NSMutableArray
NSMutableArray *quarterValueArray = [[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"] mutableCopy];
if (!quarterValueArray)
{
NSMutableArray *aQuarterArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
NSMutableDictionary *dict1 = [[[NSMutableDictionary alloc] init] mutableCopy];
[dict1 setObject:#"" forKey:#"quarterP"];
[dict1 setObject:#"" forKey:#"quartersliderValue"];
[dict1 setObject:#"" forKey:#"totalOfTheQuater"];
[aQuarterArray addObject:dict1];
}
[[NSUserDefaults standardUserDefaults] setObject:aQuarterArray forKey:#"QuarterValues"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
and then for storing slider values i am using this method in sliderviewCntroller
-(void)saveUserDefaultDataQuarter
{
NSMutableDictionary *quarter = [QuarterValueArray objectAtIndex:0];
[quarter setObject:totalQuarter1txt.text forKey:#"totalOfTheQuater"];
[quarter setObject:showQuartertxt.text forKey:#"quarterP"];
[quarter setObject:[NSString stringWithFormat:#"%d",(int)slider4.value]forKey:#"quartersliderValue"];
}
QuarterValueArray =[[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"]mutableCopy];
Why this code is crashing when the method is called second time? The crash log says:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
Try this.
NSMutableArray *quarterValueArray = [[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"] mutableCopy];
if (!quarterValueArray)
{
NSMutableArray *aQuarterArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
NSMutableDictionary *dict1 = [[[NSMutableDictionary alloc] init] mutableCopy];
[dict1 setObject:#"" forKey:#"quarterP"];
[dict1 setObject:#"" forKey:#"quartersliderValue"];
[dict1 setObject:#"" forKey:#"totalOfTheQuater"];
[aQuarterArray addObject:dict1];
}
[[NSUserDefaults standardUserDefaults] setValue:aQuarterArray forKey:#"QuarterValues"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self saveUserDefaultDataQuarter];
NSLog(#"%#",quarterValueArray);
and modify like this.
-(void)saveUserDefaultDataQuarter{
NSMutableArray*QuarterValueArray =[[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"]mutableCopy];
NSMutableDictionary *quarter = [QuarterValueArray objectAtIndex:0];
[quarter setValue:totalQuarter1txt.text forKey:#"totalOfTheQuater"];
[quarter setValue:showQuartertxt.text forKey:#"quarterP"];
[quarter setValue:[NSString stringWithFormat:#"%d",(int)slider4.value]forKey:#"quartersliderValue"]; }
UPDATE :I have used set value for setting the dictionary key values, as dictionary is already created when we are modifying values.
Alright.. Here's the thing.. I am building an app in which when the user taps on download button it downloads a bunch on images (296, to be exact).
In simulator everything works flawless, on my iPhone (4S) on around 100th image it crashes with error:
malloc: * mach_vm_map(size= "some random number") failed (error code= 3)* error: can't allocate region
libBacktraceRecording.dylib: allocate_free_list_pages() -- virtual memory exhausted!
Here's the code I wrote for downloading those images:
-(void)getData
{
NSError *error;
int i;
NSArray *brojLinije = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"linije" ofType:#"plist"]];
NSArray *urlSlike = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"urlSlike" ofType:#"plist"]];
NSArray *pocetno = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"pocetno" ofType:#"plist"]];
NSArray *sortiranje = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"sort" ofType:#"plist"]];
NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *desktopDir = [paths firstObject];
UIImage *image1 = [[UIImage alloc] init];
for (i = 0; i<296; i++) {
NSString *brojLinije1 = [NSString stringWithFormat:#"%#",[brojLinije objectAtIndex:i]];
NSString *pocetno1 = [NSString stringWithFormat: #"%#", [pocetno objectAtIndex:i]];
NSString *tableSort = [NSString stringWithFormat: #"%#", [sortiranje objectAtIndex:i]];
image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat: #"http://www.busevi.com/images/stories/Red-Voznje/Gradski-Prevoz-BG/linija.%#.png", [urlSlike objectAtIndex:i ] ]]]];
NSData *data1 = [NSData dataWithData:UIImageJPEGRepresentation(image1, 0.1)];
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
NSString *pngFilePath = [NSString stringWithFormat:#"%#%#.jpg",desktopDir,[urlSlike objectAtIndex:i]];
[data1 writeToFile:pngFilePath atomically:YES];
[newManagedObject setValue:brojLinije1 forKey:#"brojLinije"];
[newManagedObject setValue:data1 forKey:#"imageData"];
[newManagedObject setValue:pocetno1 forKey:#"pocetnoStajaliste"];
[newManagedObject setValue:tableSort forKey:#"sort"];
NSLog(#"%d / 296", i);
}
[self.managedObjectContext save:&error];
}
Only thing I know is that the image allocation frequency (too much allocating and no time to automatically release) is making the problem, AND that I have tried every method I know so far, AND I HAVE watched A LOT of "Instruments" app tutorials and only one helped (to find the source of filling virtual memory) but I still can't solve my problem.
You can use a local autorelease pool inside of the loop to immediately release any objects that were created in the body of the loop.
for (i = 0; i<296; i++) {
#autoreleasepool {
// loop body goes here...
}
}
While using SDWebImage framework I encountered a lot of bugs and unanswered crashes from the app. SDWebImage is awesome if the count of images that are about to be downloaded is lower that, let's say 50. Because, when downloading one by one image that are showed in TableViewCell + fast scrolling (fast flicking), the app receives memory warning a couple of times while the TableView is still scrolling (SDWebImage will clear memory ONLY when TableView is slowly scrolling or not scrolling at all) so it crashes the app.
So my only solace with app crashing while downloading 300+ images was simple Apple's method called dispatch_async. More about it here.
And this is how I solved my problem (I just had to move everything from my original -(void)getdata method to dispatch_async block. DONE!
So now it looks like this:
-(void)getData {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Downloading Started");
slike = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"slike" ofType: #"plist"]];
brojevi = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"linije" ofType:#"plist"]];
pocetnaStajalista = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"pocetno" ofType:#"plist"]];
sort = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"sort" ofType:#"plist"]];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Linija" inManagedObjectContext:self.managedObjectContext];
NSString *brojLinije;
NSString *pocetnoStajaliste;
NSString *sortiranje;
NSData *data;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename.png"];
for (int i = 0; i<314; i++) {
NSManagedObject *novaLinija = [[NSManagedObject alloc]initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
brojLinije = [NSString stringWithFormat:#"%#",brojevi[i]];
pocetnoStajaliste = [NSString stringWithFormat:#"%#",pocetnaStajalista[i]];
data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat: #"http://www.busevi.com/images/stories/Red-Voznje/Gradski-Prevoz-BG/linija.%#.png",slike[i]]]];
[data writeToFile:filePath atomically:YES];
sortiranje = [NSString stringWithFormat: #"%#", sort[i]];
[novaLinija setValue:data forKey:#"slikaLinije" ];
[novaLinija setValue:brojLinije forKey:#"brojLinije"];
[novaLinija setValue:pocetnoStajaliste forKey:#"pocetnoStajaliste"];
[novaLinija setValue:sortiranje forKey: #"sort"];
NSLog(#"%d / 314", i+1);
NSError *error;
[self.managedObjectContext save:&error];
} }
Hope this helps others with same problem. Good Luck and Happy Coding!
Used all of your and my suggestions and possible solutions, but, I'm sorry, none worked.
The point in this case, when app needs to download A LOT of photos, you really should use SDWebImageDownloader Class Reference.
I know It's frustrating to change the whole method and to realize the previous method (of downloading photos) is useless, so HERE is the tutorial and example on how to use SDWebImageDownloader Class. Good Luck.
I'm trying to do a list of lists... but when I use [listaCidades count] ...i'm getting throwing exception (sorry for long question but I mean that all this method is relevant for the question)
-(void) preencherCidades {
for (int iCnt = 0; iCnt < [listaEstados count]; iCnt++) {
NSString *estado = [listaEstados objectAtIndex:iCnt];
NSArray *listaNomeCidades = nil;
NSMutableArray *_listaCidades = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:estado ofType:#"txt"];
NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
listaNomeCidades = [[file componentsSeparatedByString:#"\n"]retain];
for (int iCnt2 = 0; iCnt2 < [listaNomeCidades count]; iCnt2++) {
NSArray *listaNomesPrefeitos = nil;
NSArray *listaPartidosPrefeitos = nil;
NSArray *listaVereadores = nil;
NSString *pathNomePrefeitos = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"prefeito-nome-%#",[listaNomeCidades objectAtIndex:iCnt2]] ofType:#"txt"];
NSString *fileNomePrefeitos = [NSString stringWithContentsOfFile:pathNomePrefeitos encoding:NSUTF8StringEncoding error:NULL];
listaNomesPrefeitos = [[fileNomePrefeitos componentsSeparatedByString:#"\n"]retain];
NSString *pathPartidoPrefeitos = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"prefeito-partido-%#",[listaNomeCidades objectAtIndex:iCnt2]] ofType:#"txt"];
NSString *filePartidoPrefeitos = [NSString stringWithContentsOfFile:pathPartidoPrefeitos encoding:NSUTF8StringEncoding error:NULL];
listaPartidosPrefeitos = [[filePartidoPrefeitos componentsSeparatedByString:#"\n"]retain];
NSString *pathVereadores = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"vereadores-%#",[listaNomeCidades objectAtIndex:iCnt2]] ofType:#"txt"];
NSString *fileVereadores = [NSString stringWithContentsOfFile:pathVereadores encoding:NSUTF8StringEncoding error:NULL];
listaVereadores = [[fileVereadores componentsSeparatedByString:#"\n"]retain];
Prefeito *prefeito = nil;
if([listaNomesPrefeitos count] > 0 && [listaPartidosPrefeitos count]>0)
prefeito = [[Prefeito alloc]initWithNome:[listaNomesPrefeitos objectAtIndex:0] partido:[listaPartidosPrefeitos objectAtIndex:0] id:iCnt2];
Cidade *cidade = [[Cidade alloc]initWithNome:[listaNomeCidades objectAtIndex:iCnt2] prefeito:prefeito listaVereadores:listaVereadores id:iCnt2];
[_listaCidades addObject:cidade];
}
[listaCidades addObject:_listaCidades];
}
}
By looking at your answer [Cidade isEqualToString:]:unrecognized selector to instance 0x1cec00. I came to know that that is the only point where string is expected but you are returning Object.
Its good that you solve the issue. Happy Coding.
I solved this with
NSMutableArray *listaCidades2 = (NSMutableArray *)[listaCidades objectAtIndex:indiceEstado];
Cidade *cidade = (Cidade *)[listaCidades2 objectAtIndex:row];
return cidade.nome;
instead of:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSMutableArray *listaCidades2 = (NSMutableArray *)[listaCidades objectAtIndex:indiceEstado];
return [listaCidades2 objectAtIndex:row];
}
I am reading and writing an array to NSuserdefults in this way:
indexDelete = button.tag;
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:#"save"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
myIntegers = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
myIntegers = [[NSMutableArray alloc] init];
}
[myIntegers addObject:[NSNumber numberWithInteger:indexDelete]];
NSLog (#"Array myIntegers: %#", myIntegers);
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myIntegers] forKey:#"save"];
Every thing works fine if I keep the line:
myIntegers = [[NSMutableArray alloc] init];
out of the brackets, because even if the data is there, it is not written or read correctly:
I get this error: -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
What im I doing wrong??
Change this:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myIntegers] forKey:#"save"];
to
[[NSUserDefaults standardUserDefaults] setObject:myIntegers forKey:#"save"];
if myIntegers will not be mutated (changed) in the future. If it will be changed then use:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:myIntegers] forKey:#"save"];
The ability to save arrays and dictionaries in the user defaults is documented in the class description.
You never initialize your myIntegers. The first time you get the object for key save from your defaults you get a nil object (hence not entering the if closure and allocating the structure). After that you try to archive a nil object, which is probably the cause for the error you're getting.
Try moving the initialization of the array before this and only adding the objects from the previous saving if they exist:
myIntegers = [[NSMutableArray alloc] init];
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:#"save"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
[myIntegers addObjectsFromArray:oldSavedArray];
}
I have one alternative to do this is to save mutablearray in doc dir and then retrieve it:
// Get path to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
if ([paths count] > 0)
{
// Path to save array data
NSString *arrayPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:#"array.out"];
// Write array
[yourMutableArray writeToFile:arrayPath atomically:YES];
// Read both back in new collections
NSMutableArray *arrayFromFile = [NSMutableArray arrayWithContentsOfFile:arrayPath];
for (NSString *element in arrayFromFile)
NSLog(#"Items: %#", element);
}
I wonder if it is possible to fill rangeOfString objects of a NSArray. Because I have a long list of objects for after rangeOfString:
NSArray biglist´s count is higher than list´s count.
I want to filter away the objects from the small list of the main list.
Please tell me if this is not clear.
My codes below:
NSArray *biglist = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"mainlist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSArray *list = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"smalllist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
for (NSString *listword in list);
NSMutableArray *wordlist = [[NSMutableArray alloc] init];
NSMutableArray *worindex = [[NSMutableArray alloc] init];
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
NSMutableDictionary *mutdic = [[NSMutableDictionary alloc] init];
NSMutableArray *mutarray = [[NSMutableArray alloc] init];
for (NSString *s in mainlist)
{
NSRange ran = [s rangeOfString:listword];
if (ran.location !=NSNotFound)
{
//my codes here
}
}
EDIT:
I think I can solve this by writing
int i;
for (i = 0; i<[list count]; i++)
{
NSString *same = [list objectAtIndex:i];
NSLog (#"listword: %#", same);
}
But I am not sure where to place it, inside the for loop s in mainlist or outside.
EDIT: This for loop works inside the main for loop.
EDIT:
Tried these codes, but it doesnt work somehow..
NSArray *list = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"small" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSArray *mainlist = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"mainlist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSMutableArray *large = [NSMutableArray arrayWithArray:mainlist];
NSArray *newlarge;
for (NSString *listword in list)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(SELF beginswith[c] %#)",listword];
newlarge = [large filteredArrayUsingPredicate:predicate];
}
NSLog (#"large: %#", newlarge);
NSLog (#"finished!");
"I want to filter away the objects from the small list of the main list."
If I understand correctly, you want to remove an array of items from another array. You don't want to do that much work and allocations inside an n^2 loop.
This removes an array of items from another array. Depending on how large your array is you may need to optimize further but this works:
NSArray *small = [NSArray arrayWithObjects:#"three", #"two", nil];
NSMutableArray *large = [NSMutableArray arrayWithObjects:#"one", #"two", #"three", #"four", nil];
[large removeObjectsInArray:small];
// print
for (NSString *current in large)
{
NSLog(#"item: %#", current);
}
This outputs:
2011-10-13 08:39:21.176 Craplet[5235:707] item: one
2011-10-13 08:39:21.178 Craplet[5235:707] item: four
I figured it out by myself and solved this :)
It works almost perfectly.
My codes:
NSArray *big = [[NSArray alloc] initWithObjects:#"hello ->mache", #"heisann hoppsann ->hiya", #"nei men ->da", #"however ->what", #"may ->april", #"mai ->maj", nil];
NSArray *small = [[NSArray alloc] initWithObjects: #"heisann ", #"nei men ", #"however ", #"mai", nil];
NSMutableArray *smallwithh = [[NSMutableArray alloc] init];
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
for (NSString *same in small)
{
NSLog (#"listword: %#", same);
for (NSString *s in big)
{
NSRange ran = [s rangeOfString:same];
if (ran.location !=NSNotFound)
{
[smallwithh addObject:s];
NSUInteger ind = [big indexOfObject:s];
[mindexes addIndex:ind];
}
}
}
NSLog (#"smallwith: %#", smallwithh);
[smallwithh release];
NSMutableArray *newWords =[NSMutableArray arrayWithArray: big];
[newWords removeObjectsAtIndexes: mindexes];
[big release];
[small release];
NSLog (#"newWords: %#", newWords);