How to reuse actions - iphone

Of the three parts I want to move, only the last one actually moves. I can't seem to "recycle" the ease action I created.
How is this done the correct way?
- init
{
// initial setup
[self moveParts];
}
- (void)moveParts
{
id action = [CCMoveBy actionWithDuration:1 position:ccp(0,160)];
id ease = [CCEaseInOut actionWithAction:action];
[part1 runAction:ease];
[part2 runAction:ease];
[part3 runAction:ease];
}

Seems like it is a problem with allocating and copying the actions objects. This thread answers my question pretty well, with this post being the summary:
From my experience you can't really retain actions and get any benefit out of them.
There really isn't good way to reuse actions. If you find that you are deallocing too much that the game is unstable then you should forget actions and just schedule a method that does all the work.
Examples (All variables ending with an underscore are class variables "variablename_"):
BAD WAY TO RUN ACTIONS:
.... maybe in init
moveAction_ = [[MoveBy actionWithDuration:1.f position:ccp(100.f, 0.f)]retain];
.... Some other function
[sprite1_ runAction:moveAction_];
[sprite2_ runAction:moveAction_];
That example won't work because both sprite1_, sprite2_ let's say have the same position.
They will definitely not move together! That is because the action is referenced by an actionManager and you just changed the target of that action.
Correct way:
.... maybe in init
moveAction_ = [[MoveBy actionWithDuration:1.f position:ccp(100.f, 0.f)]retain];
.... Some other function
[sprite1_ runAction:[[moveAction_ copy] autorelease]];
[sprite2_ runAction:[[moveAction_ copy] autorelease]];
Now the action will have the correct behavior but you've essentially lost all efficiency of doing a retain in the first place. You've pretty much created two new instantiations of the action.
The code above is exactly the same as:
[sprite1_ runAction:[MoveBy actionWithDuration:1.f position:ccp(100.f, 0.f)]];
[sprite2_ runAction:[MoveBy actionWithDuration:1.f position:ccp(100.f, 0.f)]];
So in the end you've gained no benefit of retaining the action. So why retain?
If you are really worried about the dealloc slowing down the game then the other way is to lose out on the convenience of actions and create your own scheduled action
...somewhere in init
[self schedule:#selector(moveStuff:)];
// do all you actions manually here
- (void) moveStuff:(ccTick) dt
{
CGPoint delta = ccp(100.f,0.f);
sprite1.position = ccp( (sprite1.position.x + delta.x * dt ),
(sprite1.position.y + delta.y * dt ) );
}
So really it's up to you to decide between speed or convenience.

Related

iOS - Amazon S3 Slow Download

I am creating an iOS app that allows users to write lets say a status update and people can comment on it, like it, interact with it in multiple ways, basically the status update has multiple properties that would be stored with it. Imagine an app with a home-screen of more than 50 of these status updates represented into a table view.
Now take your eyes away and focus on a practice/demo app, a developer trying to master the techniques before the big game (thats me!) So essentially, I have started out by setting up an S3 bucket based in Singapore. I live in Singapore, so there is a server nearby and really everything should be fast and smooth. Except, its just not. Its slow, and its starting to make me annoyed.
I know an app that uses S3 that loads high-definition panorama images with comments, likes etc. and it takes a second or a bit more for all this data to load. I am not sure how they actually carry out the process, I know they store the images on S3 but thats all I know. In my starter demo, I simply upload some pieces of text (say 20) then download them and it takes like 15 seconds under my 60 mbps wifi! These pieces of text don't even exceed a sentence each, they are phrases, so I am really kind of confused here.
I have CloudFront setup but isn't it for websites? I have the manage distributions and URL things all setup but how to setup in my code? This is probably my biggest question to nail down for release later in my other app. Even so, I live in Singapore and the bucket is in the Singapore server, so CloudFront for self-testing / practicing wouldn't be mandatory.
I find this extremely confusing, here is some annotated code I have produced, any problems, misconceptions that is leading it to be slow?
- (void)loadObjects {
S3ListObjectsRequest *listObjectRequest = [[S3ListObjectsRequest alloc] initWithName: #"testbucketquotes.rohanprostudios"];
S3ListObjectsResponse *listObjectResponse = [[AmazonClientManager s3] listObjects:listObjectRequest];
if(listObjectResponse.error != nil)
{
}
else
{
S3ListObjectsResult *listObjectsResults = listObjectResponse.listObjectsResult;
if (objects == nil) {
objects = [[NSMutableArray alloc] initWithCapacity:[listObjectsResults.objectSummaries count]];
}
else {
[objects removeAllObjects];
}
// By defrault, listObjects will only return 1000 keys
// This code will fetch all objects in bucket.
NSString *lastKey = #"";
for (S3ObjectSummary *objectSummary in listObjectsResults.objectSummaries) {
if ([[objectSummary key] rangeOfString: #"UploadedQuote"].location != NSNotFound) {
[objects addObject:[objectSummary key]];
lastKey = [objectSummary key];
}
}
while (listObjectsResults.isTruncated) {
listObjectRequest = [[S3ListObjectsRequest alloc] initWithName: #"testbucketquotes.rohanprostudios"];
listObjectRequest.marker = lastKey;
listObjectResponse = [[AmazonClientManager s3] listObjects:listObjectRequest];
if(listObjectResponse.error != nil)
{
break;
}
listObjectsResults = listObjectResponse.listObjectsResult;
for (S3ObjectSummary *objectSummary in listObjectsResults.objectSummaries) {
if ([[objectSummary key] rangeOfString: #"UploadedQuote"].location != NSNotFound) {
[objects addObject:[objectSummary key]];
}
lastKey = [objectSummary key];
}
}
if (objects.count) {
for (int i = 0; i <= objects.count - 1; i++) {
S3GetObjectRequest *req = [[S3GetObjectRequest alloc] initWithKey:[objects objectAtIndex: i] withBucket: #"testbucketquotes.rohanprostudios"];
`// asynchronously loads text (adds to operation queue)`
AsyncQuoteDownloader *quote = [[AsyncQuoteDownloader alloc] initWithRequest:req andViewController: self];
[operationQueue addOperation: quote];
// in 'AsyncQuoteDownloader' when finished calls a method in this view controller adding the response to an array and reloading a table
}
}
}
});
}
Anything wrong with my code that is making it lag so much? I would have thought this would take a matter of milliseconds if an imaging service would take 1 second to load HQ images with likes and comments etc takes 1-2 seconds.
Thank you for any help...
Update
Ok, so the iteration of keys doesn't seem to be the problem here but rather the downloading of the objects. Any thoughts? Thanks...
This is the first time I've ever even seen Objective C, so I may be completely wrong here. But... it looks like you're iterating through the keys of the entire bucket. This is going to be really, really slow with any appreciable quantity of keys.
A better design would be store a lookup table in something like DynamoDB (since you're already using AWS). You'd query there, get an array of matching id's (S3 keys), and then fetch the match objects from S3.
Another option, as I used in my own iOS app, is to use Heroku as the app layer and create a POSTGRESQL record that points to your content. This way you can create rich queries in rails and mediate upload and download with the power of Rails, rather then paying for both DynamoDB and S3.
Turns out my bucket was set to US region and not Singapore region... #doh
Its faster now, its working fine
I just needed to set the AmazonS3Client's endpoint to Singapore (SEA) region

Unsolvable memory leak IPhone

I am new to IPhone programming and am I having trouble solving the following memory leak.
while(numDeckCounter < numDecks){
int cardCounter=1;
for (int i =1; i<=52; i++) {
tempCard = [Card new]; //leaks tool says that this is leaking object
if(i>=1 && i<=13)
{
tempCard.suit = CLUBS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard]; //reference count 2
cardCounter++;
}
else if(i>=14 && i<=26)
{
tempCard.suit = DIAMONDS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
else if(i>=27 && i<=39)
{
tempCard.suit = HEARTS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
else
{
tempCard.suit = SPADES;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
if(cardCounter ==14){
cardCounter=1;
}
[tempCard release]; //this causes an EXC_BAD_ACCESS -reference count should be 1
}
numDeckCounter++;
}
I was under the impression that adding an object to the array would increase its reference count by one, then it would be safe to release the object you just added because it would not be deallocated until the array was released bumping which would then release each object in the array. This is when the object should finally be deallocated.
When I add the [tempCard release]; it crashes my app because it can't access the memory location because it has already been deallocated.
From everything I have read, I think what I said above is true. Someone please correct me if I am wrong. Thanks.
I don't see any leaks in the code you presented. (There are some opportunities to slim it down, though, say by moving the identical operations out of the conditionals).
The Leaks tool output is pretty tricky to read. Is it possible that the Card object is leaking one of it's ivars?
Instead of leaks, run static analysis on your product (Product->Analyze). I think it will flag a different part of your code.
Perhaps try [[Card alloc] init] instead of [Card new]. This is just a guess. However trying the IMO more common method of object creation could be helpful.
Check this out: Use of alloc init instead of new
You could also try removing all the code for adding the Cards to the array. So you'd essentially have:
card = [Card new];
[card release];
This could help you find memory issues associated w/ the array retaining the object perhaps?

Why does this code give EXC_BAD_ACCESS?

I am working on a game in Cocos2d for iPhone.
In my init method I have an object (type id) declared as follows (also note bossDir is declared as 1):
bossMov = [CCMoveTo actionWithDuration:1.0f position:ccp(75*bossDir, 320-55)];
[boss runAction:bossMov];
Then in a timer method I have:
if ([bossMov isDone] == YES) {
bossDir = -bossDir;
[boss stopAllActions];
[boss runAction:bossMov];
}
It moves the boss once, but after that it gives EXC_BAD_ACCESS and points me to a line in the file "CCTimer.m" that says:
if( elapsed >= interval ) {
impMethod(target, selector, elapsed); //This line in particular.
elapsed = 0;
}
How can I fix this problem?
have you tried the NSZombieEnabled because i am not a cocos2d guy but just as a suggestion i am telling you this. Because bad access error comes only when you are pointing to an object which is no longer in the memory and the application crashes if you try to do so
You are not retaining bossMov action. So when you call [boss stopAllActions]; it is released and deallocated. Then you are trying to run the deallocated action - so you get the bad access.

How to sync a section in a MutableArray with UITextView

I have one MutableArray (NSMutableArray *NoteBook;) in which I would like to store Notepages (I set up an object called NotePage.m which simply contains one NSString *noteText and a method to setNoteText). I also have an integer to keep track of my pages (NSUInteger currentPageCounter;).
I start my little program by setting up a new instance of NotePage:
NotePage *newPage = [[NotePage alloc] init];
[newPage setNoteText:#"Sample Text for a certain page..."];
I then copy this *newPage 3 times into my NoteBook Mutable Array:
[self.NoteBook insertObject:newPage atIndex:(self.currentPageCounter)];
This will give me 3 pages, at the indices 0,1,2. So far so good. Everything works splendid. But now comes the enemy, UITextView. First of all, I would like to display the contents of a page within my NoteBook in an UITextView. So I did this to sync one section in the MutableArray (e.g. page at index 0) with the UITextView which works fine:
NotePage *thisPage = [self.NoteBook objectAtIndex:self.currentPageCounter];
TextViewNote.text = thisPage.noteText;
The problem is, however, if I want to edit my UITextView and sync the updated text with the MutableArray. This is where the crashing happens... I have coded this in a separate method which can be clicked once the user is done editing the UITextView:
-(IBAction) savePage
{
NSString *tempString;
tempString = [NSString stringWithFormat:#"%#",TextViewNote.text];
NotePage *newPage = [[NotePage alloc] init];
[newPage setNoteText:tempString];
[self.NoteBook insertObject:newPage atIndex:(self.currentPageCounter)]; // e.g. at index 0 for page 1
[self.NoteBook removeObjectAtIndex:(currentPageCounter+1)]; // i.e. this is to remove the old page 1 which used to be at index 0 but now is at index 1
[self syncTextView];
[self syncCounter];
}
I guess there is a less cumbersome way (I'm still a beginner...) to simply replace an object at a certain index at a Mutable Array. As it stands now, it will simply crash once I try to move onwards to the next index position which is apparently not found anymore.
Thanks for any ideas!
I guess there is a less cumbersome way
(I'm still a beginner...) to simply
replace an object at a certain index
at a Mutable Array.
Indeed. Look at -[NSMutableArray replaceObjectAtIndex:withObject:].
Considering the error you posted, it sounds like you've got a bad pointer somewhere. The error is complaining that somewhere, a -[UITouchData length] message is being sent to an instance of NotePage. Turn on the 'Stop on Objective-C Exceptions' option in the debugger (under the Run menu). That should help you see where the problem is occurring.
NSMutableArray has a method replaceObjectAtIndex:withObject: that should do what you are trying to do.
You've got a memory management issue, however, unless you are calling release on the newPage after you add it to the array. Unless you are planning on making the NotePage class more complex, it might make sense simply to change the item's text rather than substituting a new object:
[[self.noteBook objectAtIndex:currentPageCounter] setNoteText:tempString];
(Also, be aware that inserting the newPage object into the array four times does not copy the object; it simply inserts the same reference four times. If you want four distinct objects, you will need to allocate four objects in a loop:
for(i = 0; i < 4; i++){
NotePage *newPage = [[NotePage alloc] init];
[newPage setNoteText:#"Dummy text"];
[self.notebook addObject:newPage];
[newPage release];
}

Parsing my flat-file on iPhone is a pain in the ***, please Help me out

I am programming an iPhone App which is supposed to parse a flat-file from the web, create managed objects from the flat-file and later on should display them in an UITableView.
There are no problems with the saving and the displaying, but I just can't get the hang of a good Parser.
Thats the file I want to parse: Flat-file
AS far as I know, I can't use the NSXMLParser for this task (because obviously there are no tags).
So I at first tried to programm a NSScanner which should get me the interesting properties --> didn't work out
Now I am using this method:
- (void) parseMemberDataWithURL: (NSString *)urlString
{
self.memberTempCounter = 1;
//Get data from web
self.downloadedText = [NSString stringWithContentsOfURL: [NSURL URLWithString: urlString] encoding:NSUTF8StringEncoding error:nil ];
memberArray = [downloadedText componentsSeparatedByString:#";"];
while (self.memberTempCounter<[memberArray count])
{
[[ExhibitorController sharedController] createExhibitorWithName:[memberArray objectAtIndex:self.memberTempCounter]
street:[memberArray objectAtIndex:self.memberTempCounter+2]
zip:[memberArray objectAtIndex:self.memberTempCounter+3]
city:[memberArray objectAtIndex:self.memberTempCounter+4]
email:[memberArray objectAtIndex:self.memberTempCounter+7]
phone:[memberArray objectAtIndex:self.memberTempCounter+5]
website:[memberArray objectAtIndex:self.memberTempCounter+8]
produktbereiche:[[memberArray objectAtIndex:self.memberTempCounter+9] componentsSeparatedByString:#","]];
self.memberTempCounter= self.memberTempCounter+13;
}
}
I am using the memberTempCounter to identify the property.
The problems are:
This only works out in like 3 of 4 times.1 of 4 times the App crashes and I have no Idea why...
The method has a performance like a 1962 VW Beetle. Parsing the whole chunk of data takes up to 3 Minutes on my iPhone 3G
Any Ideas or a simpler way to do this?
I would be really gratefull. Thanks in advance: -)
You might as well do all the parsing in the background, and then display as the information gets parsed.
As for memory issues, try doing temporary autorelease pools and release every 50 or so iterations through the loop.
int count = 0;
NSAutoreleasePool * loopPool = [[NSAutoreleasePool alloc] init];
while(someInsanelyLargeCondition){
// Do your stuff here
// .............
count++;
if (count > 50) {
count = 0;
[loopPool release];
loopPool = [[NSAutoreleasePool alloc] init];
}
}
Recursive-descent (LL1) parsers are pretty simple, light on memory, and for speed they go almost as fast as you can run a pointer through characters. Building your data structure would probably be the dominant time-taker.
I was finally able to fix my performance problem.
I have a method in another class, which ads Tags for the different Exhibitors. Therefore it first checks if the Tag already is stored in the database or else creates it.
With an growing Set of Tags in my database the search-process took longer and longer and this led to the long parsing time.
Anyone else having this problem: Take a look at the Performance Core Data Programming guide of apple in the "Implementing Find-or-Create Efficiently"-section:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html