正在从数据库或其他来源检索 phone 个号码到 AddBlockingEntry

Retrieving phone numbers from database or other source to AddBlockingEntry

我正在学习 Xamarin 的示例,了解如何使用呼叫目录扩展 (full example here) 在 iOS 10 的应用程序中阻止 phone 号码。

下面是我添加一个phone数字来阻止的代码(基于Xamarin的例子):

bool AddBlockingPhoneNumbers(CXCallDirectoryExtensionContext context)
{
    // This logging works, log written to database.
    _logService.Log("Start adding numbers");

    // Hardcoding phone numbers to add works.
    var phones = new List<Phone> { 
        new Phone { 
            PhoneNumber = 14085555555, 
            CompanyName = "Bjarte" } 
    };

    // When I uncomment the following line, the 
    // extension crashes here, I never get to the
    // logging below.
    //List<Phone> phones = _phoneService.GetPhones();

    foreach (var phone in phones)
    {
        context.AddBlockingEntry(phone.PhoneNumber);
    }

    _logService.Log("Finished adding numbers");
    return true;
}

为了在应用程序和扩展程序之间进行通信,我设置了一个具有共享目录的应用程序组。这里我有一个 SQLite 数据库,应用程序和扩展程序都可以成功写入。例如,我用它来记录日志,因为我不能直接调试扩展。

在这个数据库中,我有 phone 个我想阻止的号码。

这是我从数据库中检索 phone 号码的方法。我正在使用 NuGet 包 sqlite-net.

public List<Phone> GetPhones()
{
    var phones = new List<Phone>();

    using (var db = new SQLiteConnection(DbHelper.DbPath()))
    {
        var phoneTable = db.Table<Phone>().OrderBy(x => x.PhoneNumber);
        foreach (var phone in phoneTable)
        {
            phones.Add(new Phone
            {
                PhoneNumber = phone.PhoneNumber,
                CompanyName = phone.CompanyName
            });
        }
    }

    return phones;
}

到目前为止,如果我将号码硬编码到 AddBlockingPhoneNumbers 方法,我只能设法阻止 phone 个号码。

有没有人幸运地从外部来源检索 phone 号码?数据库、文件还是其他?

我还没弄清楚为什么,但我的应用程序扩展无法从我的 sqlite 数据库中读取数据,只能写入它。我的疯狂猜测是,这是因为扩展程序对其允许执行的操作比应用程序有更严格的限制。

但是,如果我用纯文本文件替换数据库,只要文件足够小,它就可以工作。

我将我的 phone 号码列表分成单独的文件,每个文件有 1000 phone 个号码。好像有用。

是的,Call Directory Extensions 的内存非常有限。我的经验是,在分配内存时必须非常保守,而在显式释放内存时必须非常积极。