How to limit download time in ios? [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to limit download time in ios?
I need :
if download time from url to ImageView more than 2 second, download from another url.

Set the timeout to a shorter time, 2 seconds:
let request = NSMutableURLRequest()
request.timeoutInterval = 2.0
Timeout reference

If you download images asynchronously, you can use this method (together with if):
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(numberInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
print("Delayed job")
}
So if numberInSeconds is 2, the code will execute after 2 seconds.

Related

How do I schedule a repeating (daily) background task? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 13 days ago.
The community is reviewing whether to reopen this question as of 12 days ago.
Improve this question
I want to send the user a notification at the same time every day.
I know that I can schedule a repeating notification, but that won't work in my case because the notification content is dynamic.
So, I think I need to use background tasks. But there is no repeat parameter on the background task scheduler.
My solution is to try to use recursion to schedule the next bg task from within the bg task. On app launch, I'll check to make sure the recursion chain is still alive and update it if it has been broken for whatever reason.
Here is my current code. Besides one error with async code in the launch handler, am I on the right track? I still can't wrap my head around the need to register, request AND submit, but the code looks right to me, based on the Swift docs and online examples.
Also, I'm not sure what to do about the unaccepted awaits. The error is Cannot pass function of type '(BGTask) async -> Void' to parameter expecting synchronous function type. It wants synchronous code in the register handler.
import BackgroundTasks
import CoreData
// This is an attempt to maintain a daily repeating background task. You can't tell the system to repeat the task, like you can with notifications, so we use recursion.
// There is a chance that the recursion chain will break if something goes wrong, so we check for that at app launch and handle it.
// The problem is that some users may not launch the app often. But let's try it out and see if it becomes a problem.
func scheduleDailyBackgroundTask (moc: NSManagedObjectContext, isRecursiveCall: Bool) async -> Void {
// Check to see if there is already a bg task scheduled
let pendingTasks = await BGTaskScheduler.shared.pendingTaskRequests()
// If so, return early
if pendingTasks.count > 0 {
return
}
// Schedule the task
BGTaskScheduler.shared.register(forTaskWithIdentifier: "SCHEDULE_DAILY_NOTIFICATION", using: nil) { task in
let request = BGAppRefreshTaskRequest(identifier: "SCHEDULE_DAILY_NOTIFICATION")
// If we're starting the recursion chain, we don't want a delay. But if this has been called by a background task, aim for 20 hours
if isRecursiveCall {
request.earliestBeginDate = .now.addingTimeInterval(20 * 3600)
}
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule daily background task: \(error)")
}
task.expirationHandler = {
print("Daily background task expired.")
}
await scheduleDailyNotification(moc: moc)
await scheduleDailyBackgroundTask(moc: moc, isRecursiveCall: true)
task.setTaskCompleted(success: true)
}
}

Update Once each in specific time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
parallax.update(currentTime)
}
How can i control the update to be done once over a specific period of time
Your code doesn't add much to your question, so I don't really know if this is what you are looking for, but here is some code to call a function every X time (swift 3):
// call MyMethod every 1 second :
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(MyController.myMethod), userInfo: nil, repeats: true)
Please search before you post a question -> answer

Run a method from another method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am new to programming, but I can't seem to get this to work.
When I try to run the method from another method every thing stops
-(void)rotateMmovment {
}
-(void)stickMove {
[self rotateMmovment];
stick.center = CGPointMake(stick.center.x + x, stick.center.y);
}
First you should check either your method is running or not via using NSLog.its does't seems that you are facing problem due to calling method which have empty body
-(void)rotateMmovment {
NSLog(#"My method is running");
}
-(void)stickMove {
[self rotateMmovment];
stick.center = CGPointMake(stick.center.x + x, stick.center.y);
}

Getting error when trying to pass integer to method - iOS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a UIButton which when pressed passes an integer to a simple method I have set up. However I keep on getting this error:
Implicit conversion of 'int' to 'id' is disallowed with ARC
Here is my code:
[self performSelector:#selector(show:) withObject:prev_image afterDelay:2.0];
The reason I'm not just doing [self show:prev_image] is because I want a delay before the method is called.
Thanks for your time, Dan.
You have two choices:
Change the show: method to take an NSNumber and then wrap prev_image in an NSNumber or
Use dispatch_after.
Code:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self show:prev_image];
});
prev_image is an int where an 'object' must be passed into the performSelector method.
I would advise that you do this:
[self performSelector:#selector(show:) withObject:#(prev_image) afterDelay:2.0f];
I would also recommend you change prev_image to prevImage whilst programming in Objective-C simply for style.
Using dispatch_after is not necessary here and you almost certainly want to stay as high level as possible when tackling problems in iOS development.
I apologise for being unclear.
You will also want to change the method signature and implementation of show:
- (void)show:(NSNumber *)number
{
NSInteger integerNumber = [number integerValue];
}
One solution is to use [NSNumber numberWithInt:prev_image] instead of prev_image. Also you'll need to change the show: method to to take an NSNumber instead of an int.

How do i keep the label updated as the time passes in ios [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do i keep the UILabel updated as the time passes when a post is posted in ios ..like if >its posted just now it should say 0s and as time passes it changes to >1min,2min...1hr,2hr...1day and so in ios
I am new to the ios development
Thanks for any help!
You need to use NSTimer for this purpose if I understand your questions properly.
Doing something on a regular interval is pretty easy in iOS.
In your main code:
[NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: #selector(doSomething:)
userInfo: nil
repeats: YES];
Then add the doSomething method:
- (void)doSomething:(NSTimer *)timer {
NSLog(#"We did it!");
}
First of all you need to keep the date when user posted. You can easily do that by using:
NSDate* postedDate = [NSDate now];
Than if you want to find a seconds left from that time you can use:
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:postedDate];
This gives you seconds from posting date - you need to format output.
For keeping label updating itself you should use NSTimer:
[NSTimer scheduledTimerWithTimeInterval: 1.0f
target: self
selector: #selector(updateLabel:)
userInfo: nil
repeats: YES];
Where updateLabel will format "timeDiff" into something human readable like 1minute / day etc...

Resources