function that runs all the time in xcode? - xcode

I want to make a function that is always running in xcode similar to Unity's "function Update()". is this possible and if so, how.

You'll want to read up on run loops and NSTimers.

This code work for me,Hope it will help you!
write this function in your viewDidLoad() method
[self performSelector:#selector(onTick:) withObject:nil afterDelay:2.0];
And Here is the onTick: Method do whatever you want to run all the time
-(void)onTick:(NSTimer *)timer
{
//do something here
}

Related

Unity LookAtMouse function kills movement performance

private void LookAtMouse()
{
if (Physics.Raycast(mainCamera.ScreenPointToRay(mouseLook), out var raycastHit, Mathf.Infinity))
{
_direction = (raycastHit.point - transform.position).normalized;
_direction.y = 0;
transform.forward = _direction;
}
}
whenever I call this function in update it kills my movement. If I don't run this function everything works perfectly. I have tried using transform.LookAt() I tried using transform.Translate() in movement, Rigidbody.MovePosition(), changing anything else doesn't help.
If I comment out the transform.forward = _direction; then everything works perfect. what is it about that that makes my movement run game breakingly slow
I have tried every possible thing I can find online to fix this and nothing has been helping.
As soon as any physics is involved you do not want to set things via Transform as this breaks the physics and collision detection.
You should be using Rigidbody.MoveRotation in combination with Quaternion.LookRotation within FixedUpdate
Try to call it in FixedUpdate
generally, I use Update just for catching Key input because it may be called +1000 time if your device is powerful, in the other hand fixed update is better to do the physics .iv it a try and replay if it didn't help to think in other solution;

Creating a Neat delay function using NSTimer - Swift

I am trying to create a simple and neat function that uses NSTimer to add a delay to what is ever in the brackets. For e.g.
func Adddelay(delay:Double, closure:()->()) {
//Code in here using NSTimer that will add a delay for as long as stated in delay (the number passed in)
}
From this, i am trying to aim to use it by going:
delay(1){
//Code in here that will execute after a delay of one second
}
The function NEEDS to use NSTimer for what i am using it for as a whole.
Any help will be greatly appreciated.
in your case, you need to use extension for NSTimer. Published on gist: https://gist.github.com/dimpiax/e2adc74c25d76d124fc3
Update:
Published lib: https://github.com/dimpiax/RichTimer

CMPedometer is not running

My CMPedometer is not running.
The code before it and after it gets run, but it itself does not work.
I get no warning or exception.
I'm testing it on a real 5s.
I've tried both querydata and startpedometerupdates.
I am importing core motion and the library is linked.
Any help?
if ([CMPedometer isStepCountingAvailable] == YES)
{
CMPedometer *cmped;
[cmped queryPedometerDataFromDate:start toDate:[NSDate date] withHandler:^(CMPedometerData *pedometerData, NSError *error){
stepslabel.text = [pedometerData.numberOfSteps stringValue];
}];
}
The problem with the original code above is the cmped variable gets deallocated at the end of the if statement, so the query is destroyed before it finishes.
By changing it to a strong property, it is retained in memory for the life of the class.
It seems really odd but I got it working by not declaring in the .h or before using it. What worked was declaring as #property CMPedometer *cmped; right after interface

mergeChangesFromContextDidSaveNotification taking up all space

I'm implementing iCloud + Core Data for Mac OS and I'm having a major issue with mergeChangesFromContextDidSaveNotification.
When NSPersistentStoreDidImportUbiquitousContentChangesNotification is posted, I'm calling the following method:
- (void)mergeChangesFromNotification:(NSNotification *)note
{
self.managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
[self.managedObjectContext performBlock:^{
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:note];
}];
// ....
}
The problem is that mergeChangesFromContextDidSaveNotification: does not return and, also, takes up more and more memory until the system runs out space.
Any thoughts on what the problem might be? I'm doing almost the same thing on iOS and works just fine.
Thanks!
I finally found the bug - and as I expected, it was quite stupid one:
I was merging the changes into the wrong context.

gnustep runloop

Does anyone tell is GNUstep NSRunLoop extension functions:
(void) addEvent: (void*)data
type: (RunLoopEventType)type
watcher: (id)watcher
forMode: (NSString*)mode;
this method is work?, and how to setup the argument data? I set a pipe to read file description, but it seems not work fine. Really hard to figure out the question, can anyone help me?
This method works. Please show more of your code.
eg. for reading,
[[NSRunLoop currentRunLoop] addEvent: (void*)anFd
type: ET_RDESC
watcher: anObj // must implement receivedEvent:type:extra:forMode:
forMode: NSDefaultRunLoopMode]; // rtfm

Resources