I have a sample test project at [grdb_test](https://github.com/EricG-Personal/grdb_test.git)
This project uses GRDB and GRDBCombine which are SQLite related frameworks supporting SwiftUI and Combine.
On the interface, I have two lists. The first list displays a complete list of strings. The second list displays only the unique strings.
In Tests.swift, I have two publishers which work and support both of those lists:
func allTheTestsPublisher() -> DatabasePublishers.Value<[Test]>
{
ValueObservation
.tracking(value:
{
db in
let bestTests = try Test.fetchAll( db )
return bestTests
})
.publisher( in: database )
}
func allTheUniqueTestsPublisher() -> DatabasePublishers.Value<[Test]>
{
ValueObservation
.tracking(value:
{
db in
let bestTests = try Test.fetchAll( db )
return bestTests
})
.map { (theTests: [Test]) -> [Test] in
var uniqueTests: [Test] = []
for aTest in theTests
{
var found = false
for uniqueTest in uniqueTests
{
if uniqueTest.name == aTest.name
{
found = true
break
}
}
if found == false
{
uniqueTests.append( aTest )
}
}
return uniqueTests
}
.publisher( in: database )
}
The primary problem I would like to solve is that I am doing two complete queries for all of the Tests in the database when only one should be necessary.
I have tried variants of:
func allTheUniqueTestsPublisher() -> DatabasePublishers.Value<[Test]>
{
let publisher = allTheTestsPublisher().map { (theTests: [Test]) -> [Test] in
var uniqueTests: [Test] = []
for aTest in theTests
{
var found = false
for uniqueTest in uniqueTests
{
if uniqueTest.name == aTest.name
{
found = true
break
}
}
if found == false
{
uniqueTests.append( aTest )
}
}
return uniqueTests
}
return publisher
}
but that generates the compile error:
Cannot convert return expression of type 'Publishers.Map<DatabasePublishers.Value<[Test]>, [Test]>' to return type 'DatabasePublishers.Value<[Test]>'
which I am not sure how to resolve.
I would appreciate any thoughts people might have.
(As a side note, I would have liked to be able to use Set() to do the unique test for me, but I have not figured out if that is possible since Set considers the id as well as the name when only the name matters.)