yellowV2ex
V2EX  ›  问与答

objc如何获取一个固定的非主线程?

  •  
  •   yellowV2ex · Apr 24, 2013 · 3264 views
    This topic created in 4792 days ago, the information mentioned may be changed or developed.
    由于多个栏目,需要同时初始化并读sqlite的表,内容浏览时有事也会一起更新多个表。
    现在是用 `performSelectorInBackground` 读取数据库,完成后通知主线程做一些操作,
    但多个 `performSelectorInBackground` 同时读数据库时,有时就会报错跳出,断点的位置返回是数据库正在操作或锁定无法介入。

    原因大概是多个线程同一时间对数据库进行操作,导致冲突报错。
    全部数据库都改在主线程 `performSelectorOnMainThread` 的话,他们就会顺序执行不会报错,但在主线程他们就会导致界面卡顿。

    如何获取一个固定非主线程,然后让一些方法都到一个固定的线程里执行呢?
    4 replies    1970-01-01 08:00:00 +08:00
    alexrezit
        1
    alexrezit  
       Apr 24, 2013   ❤️ 1
    用一个 shared instance, 自己创建一个 queue.
    RoCry
        2
    RoCry  
       Apr 24, 2013   ❤️ 1
    dispatch_queue_t theQueue = dispatch_queue_create("com.rocry.someQueueName", DISPATCH_QUEUE_SERIAL);
    ytzong
        3
    ytzong  
       Apr 24, 2013 via iPad
    果断Fmdb的queue啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2427 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 16:04 · PVG 00:04 · LAX 09:04 · JFK 12:04
    ♥ Do have faith in what you're doing.