面试题iOS面试相关iOS Developer

一次面试的记录

2017-08-11  本文已影响597人  Mee_Leo

面试记录

提到以下问题:

    syncSerialBlock
    {
                dispatch_queue_t queue = dispatch_queue_create("yanhooQueue", DISPATCH_QUEUE_SERIAL);

        dispatch_sync(queue, ^{
        NSLog(@"1-----%@", [NSThread currentThread]);

        // 这里阻塞了
            dispatch_sync(queue, ^{
                NSLog(@"2-----%@", [NSThread currentThread]);
        });
      });
        }
        /// 或者
    -(void)syncMain
    {
    // 获得主队列
    dispatch_queue_t queue = dispatch_get_main_queue();

    // 这里阻塞了
    dispatch_sync(queue, ^{
        NSLog(@"1-----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"2-----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"3-----%@", [NSThread currentThread]);
    });
}

> 不要在串行队列(先进先出)中同步调度任务,可以避免死锁。
vm_size_t usedMemory(void) {
    struct task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
    return (kerr == KERN_SUCCESS) ? info.resident_size : 0; // size in bytes
}

    vm_size_t freeMemory(void) {
    mach_port_t host_port = mach_host_self();
    mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    vm_size_t pagesize;
    vm_statistics_data_t vm_stat;

    host_page_size(host_port, &pagesize);
    (void) host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
    return vm_stat.free_count * pagesize;
    }

    void logMemUsage(void) {
    // compute memory usage and log if different by >= 100k
    static long prevMemUsage = 0;
    long curMemUsage = usedMemory();
    long memUsageDiff = curMemUsage - prevMemUsage;

    if (memUsageDiff > 100000 || memUsageDiff < -100000) {
        prevMemUsage = curMemUsage;
        NSLog(@"Memory used %7.1f (%+5.0f), free %7.1f kb", curMemUsage/1000.0f, memUsageDiff/1000.0f, freeMemory()/1000.0f);
    }
    // 周期性获取目前占用的内存,设定阈值,如果较大,记录并上报堆栈信息
    [NSThread callStackSymbols]);
    
    }
    
///以下是帧率的获取方法
    let displayLink CADisplayLink(
            target: urDelegate,
            selector: #selector(urFunc)
        )
    //每帧刷新都会回调 urFunc ,
    func urFunc() {
         if lastNotificationTime == 0.0 {
            lastNotificationTime = CFAbsoluteTimeGetCurrent()
            return
        }

        numberOfFrames += 1

        let currentTime = CFAbsoluteTimeGetCurrent()
        let elapsedTime = currentTime - self.lastNotificationTime

        if elapsedTime >= self.notificationDelay {
            
          let fps = Int(round(Double(self.numberOfFrames) / elapsedTime))
            lastNotificationTime = 0.0
            numberOfFrames = 0
            print(fps) //帧率
        }
        
    }
上一篇下一篇

猜你喜欢

热点阅读